Code example for PushNotifications



Discussions regarding 1C:Enterprise for mobile devices with Android, iOS, or Windows Phone.

#1
People who like this:0Yes/0No
Active user
Rating: 4
Joined: Sep 1, 2014
Company: Smart ID

Hello Timofey.

Can you give me a code sample for using PushNotification service on local mobile platform and between mobile application and main infobase on the server?

Thanks in advance.

 
#2
People who like this:0Yes/0No
Administrator
Rating: 100
Joined: Jul 20, 2011
Company: 1C Company

Hello, Marius!

Sorry for the late reply. Here are examples of sending and receiving push notifications:

Sending push notifications.

Code
// Creating push notification.
Notification = New DeliverableNotification;

// SubscriberID - ID of the notification recipient, that you have to get from 
// the recipient using any available method.
Notification.Recipients.Add(SubscriberID);
Notification.Text = NStr("en = 'Some sample text'");
Notification.SoundAlert = SoundAlert.None;
Notification.Badge = 2;

// Sending push notification.
// AuthenticationData - depends on the recipient's OS.
//    - for Android - the GCM service connection string.
//   - for iOS     - the APNs connection certificate file.
DeliverableNotificationSend.Send(Notification, AutenticationData);

Receiving push notifications.
Code
Procedure OnStart()
   
   // Get your ID and send to the sender using any available method.
   ApplicationNumber = "1234567890";
   SubscriberID = DeliverableNotifications.ReceiveNotificationSubscriberID(ApplicationNumber);
   // ...
   
   // Attaching the push event handler on the application start.
   DeliverableNotifications.AttachNotificationHandler("OnReceiveNotification");
   
EndProcedure

// Handling the received notification.
Procedure OnReceiveNotification(Notification, IsLocal, IsDisplayed) Export
   
   If IsLocal Then
      Message(StringFunctionsClientServer.SubstituteParametersInString(
         NStr("en = 'Received a local notification with following text: %1'"), Notification.Text);
   Else
      Message(StringFunctionsClientServer.SubstituteParametersInString(
         NStr("en = 'Received a push notification with following text: %1'"), Notification.Text);
   EndIf;
   
EndProcedure

1C Company support team
 
#3
People who like this:0Yes/0No
Active user
Rating: 4
Joined: Sep 1, 2014
Company: Smart ID

Hello Timpofey.

This code doesn`t work:

Code
#If MobileAppClient Then
      DeliverableNotifications.AttachNotificationHandler("ReceivePushNotification");
   #EndIf   



It says that "ReceivePushNotification" is wrong parameter for the DeliverableNotifications.AttachNotificationHandler function.

I also tried with NotificationDescription but the error is the same.

 
#4
People who like this:0Yes/0No
Administrator
Rating: 100
Joined: Jul 20, 2011
Company: 1C Company

Does ReceivePushNotification() have 3 parameters and is available for mobile application?

1C Company support team
 
#5
People who like this:0Yes/0No
Active user
Rating: 4
Joined: Sep 1, 2014
Company: Smart ID

Yes.

The code is in Managed Application module.

The code is:

Code
// Handling the received notification.
Procedure ReceivePushNotification(Notification, IsLocal, IsDisplayed)
   
   If IsLocal Then
         Message("Received a local notification with following text:" + Notification.Text);
   Else
        Message("Received a push notification with following text:" +  Notification.Text);
   EndIf;
   
EndProcedure

Procedure OnStart()
   
   SetApplicationCaption("Smart Selling " + Commons.GetVersion());
   
   #If MobileAppClient Then
      DeliverableNotifications.AttachNotificationHandler("ReceivePushNotification");
   #EndIf   

EndProcedure

Edited: Marius Gidu - Feb 12, 2016 04:46 AM
 
#6
People who like this:0Yes/0No
Administrator
Rating: 100
Joined: Jul 20, 2011
Company: 1C Company

Hello, Marius Gidu!

Try adding Export to the handler procedure:

Code
Procedure ReceivePushNotification(Notification, IsLocal, IsDisplayed) Export

1C Company support team
 
#7
People who like this:0Yes/0No
Active user
Rating: 4
Joined: Sep 1, 2014
Company: Smart ID

I added Export and it was ok.

But how I can getr SubscriberID, ApplicationNumber and AutenticationData to implement this both on the server and the mobile devices?

I want to send notifications from server to mobile devices to notifi about changes in main infobase.

How to implement this?

 
#8
People who like this:0Yes/0No
Administrator
Rating: 100
Joined: Jul 20, 2011
Company: 1C Company

Hello, Marius.

Here is that part:

Code
   // Get your ID and send to the sender using any available method.
   // You need to use some string as an identifier of the application. Maybe an email or login of a user.
   ApplicationNumber = "1234567890"; 
   SubscriberID = DeliverableNotifications.ReceiveNotificationSubscriberID(ApplicationNumber);
   // Send the subscriber ID to the sender, for example using web service of the server application.
   // ...

1C Company support team
 
#9
People who like this:0Yes/0No
Active user
Rating: 4
Joined: Sep 1, 2014
Company: Smart ID

Thanks.

And code for the server?

 
#10
People who like this:0Yes/0No
Administrator
Rating: 100
Joined: Jul 20, 2011
Company: 1C Company

Marius,

On server you should call those PUSH notification services:
Apple Push Notification Service
Google Cloud Messaging

1C Company support team
 
#11
People who like this:0Yes/0No
Active user
Rating: 3
Joined: Dec 4, 2015
Company: Smart ID

Hello Timofey,

I tried to use the following code in managed application module and all it gives me are errors. Can you help me please?

Procedure OnStart()

// Get your ID and send to the sender using any available method.
ApplicationNumber = "1234567890";
SubscriberID = DeliverableNotifications.ReceiveNotificationSubscriberID(ApplicationNumber);
// ...

EndProcedure

Error:
msg "{ManagedApplicationModule(7)}: Error calling context method (ReceiveNotificationSubscriberID): Cannot get the push notification subscriber ID." String

 
#12
People who like this:0Yes/0No
Administrator
1C:Professional
Rating: 10
Joined: Jun 25, 2013
Company: 1C Company

Hello, Sebastian Dan,

ReceiveNotificationSubscriberID is available for mobile application (client) only.

Do you try to debug the application on a desktop?
Use IfMobileClient directive in this case.

1C Company support team
 
#13
People who like this:0Yes/0No
Active user
Rating: 3
Joined: Dec 4, 2015
Company: Smart ID

Hello Sergey,

I know it is available only for mobile client application. What I did was to put this piece of code on managed application module OnStart():

#If MobileAppClient Then
try
AppId = "2837683276432";
DeviceToken = DeliverableNotifications.ReceiveNotificationSubscriberID(AppId);
except
Message(ErrorDescription());
EndTry;
#Endif

 
#14
People who like this:0Yes/0No
Administrator
1C:Professional
Rating: 10
Joined: Jun 25, 2013
Company: 1C Company

Hello, Sebastian,

I'm afraid, we've got not enough details to help you. Can you provide us with .cf file of your application? Please send it to int@1c.ru for Sergey Polikarpov if you do.

1C Company support team
 
Subscribe