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 Dynamics

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
Timofey Bugaevsky
Guest

Joined:
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

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

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
Timofey Bugaevsky
Guest

Joined:
Company:

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

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

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
Timofey Bugaevsky
Guest

Joined:
Company:

Hello, Marius Gidu!

Try adding Export to the handler procedure:

Code
Procedure ReceivePushNotification(Notification, IsLocal, IsDisplayed) Export

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

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
Timofey Bugaevsky
Guest

Joined:
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.
   // ...

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

Thanks.

And code for the server?

 
#10
People who like this:0Yes/0No
Timofey Bugaevsky
Guest

Joined:
Company:

Marius,

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

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

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
Active user
1C:Professional
Rating: 8
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
1C:Professional
Rating: 3
Joined: Dec 4, 2015
Company: Smart ID Dynamics

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
Active user
1C:Professional
Rating: 8
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
 
#15
People who like this:0Yes/0No
Active user
Rating: 4
Joined: Mar 14, 2012
Company: GSPD

I write as this:
UseGCM = Constants.UseGCM.Get();
Notification      = New DeliverableNotification;
Notification.Title   = "Test";
Notification.Text   = "Content";
Notification.SoundAlert = SoundAlert.Default;

Records = InformationRegisters.MáyTrạm.Select(,);
While Records.Next() Do
Notification.Recipients.Add(Records.NotificationID.Get());
Message("Added");
EndDo;
AuthenticationData = New Map();
If UseGCM = True Then
AuthenticationData[DeliverableNotificationSubscriberType.GCM] = Constants.GCMSenderServerKey.Get();
EndIf;
DeliverableNotificationSend.Send(Notification, AuthenticationData);

----------------------
When program is running, it shown the error: "wrong parameter 2" (AuthenticationData)
I do not know why? (I have registered Google Key)

Edited: Ninh Giang Thi - Dec 19, 2018 04:12 AM
 
#16
People who like this:0Yes/0No
Interested
Rating: 16
Joined: Dec 4, 2017
Company:

Dear Ninh Giang Thi,

It seems that you actually use the FCM sender key.

https://stackoverflow.com/questions/37789264/api-key-for-gcm-is-suddenly-invalid-unauthorized-401-error


FCM service support is available in 1C:Enterprise mobile platform 8.3.13 and above.

Best regards,
Vladimir Gurov

1C Company support team
 
#17
People who like this:0Yes/0No
Active user
Rating: 4
Joined: Mar 14, 2012
Company: GSPD

Thank you, I am done for Android.
Now I can ask you about notification on iOS, I do not know how to get iOS Certificate (APNS)?

 
#18
People who like this:0Yes/0No
Interested
Rating: 16
Joined: Dec 4, 2017
Company:

Dear Ninh Giang Thi,

You can take a look at this article:

https://1c-dn.com/library/mobile_application_development/working_with_user­_notifications_in_mobile_applications/

Best regards,
Vladimir Gurov

1C Company support team
 
#19
People who like this:0Yes/0No
Active user
Rating: 4
Joined: Mar 14, 2012
Company: GSPD

Thank you, I have created APNS certificate.
But now, the command "SubscriberID = DeliverableNotifications.ReceiveNotificationSubscriberID("");" have error "Cannot receive name (ID) of Push register" on iOS device.
Help me, please!

 
#20
People who like this:0Yes/0No
Just came
Rating: 1
Joined: Oct 30, 2012
Company: DND YAZILIM LTD.

Hi,

I can't do push notification on IOS Device. Please show example code and screenshot.

 
#21
People who like this:0Yes/0No
Interested
Rating: 16
Joined: Dec 4, 2017
Company:

Dear Ninh Giang Thi,

If your request is still actual, please send us your code snippet and description of the issues you encountered.

Best regards,
Vladimir Gurov

1C Company support team
 
#22
People who like this:0Yes/0No
Active user
Rating: 4
Joined: Mar 14, 2012
Company: GSPD

Here is the code:

#If MobileClient Then
DeliverableNotifications.AttachNotificationHandler("ĐọcThôngBáo");
ApplicationNumber = DùngChung.ỨngDụngGoogle();
If DùngChung.NSDIOS(NSDHiệnTại) Then
//This command notice error when It run on iOS system
SubscriberID = DeliverableNotifications.ReceiveNotificationSubscriberID("");
DùngChung.LưuSID(SubscriberID, NSDHiệnTại);
Else
SubscriberID = DeliverableNotifications.ReceiveNotificationSubscriberID(ApplicationNumber);
DùngChung.LưuSID(SubscriberID, NSDHiệnTại);
EndIf;
#EndIf

 
#23
People who like this:0Yes/0No
Interested
Rating: 16
Joined: Dec 4, 2017
Company:

Dear Ninh Giang Thi,

We found no issues in this piece of code.

To let us investigate the issue thoroughly, please send a copy of your infobase (.dt).

Best regards,
Vladimir Gurov

1C Company support team
 
#24
People who like this:0Yes/0No
Active user
Rating: 2
Joined: Oct 14, 2019
Company: 1c Vietnam

Dear Vladimir,
The link below doesn't work, can you fix it please.
Duc Tien

Quote
Vladimir Gurov wrote:
Dear Ninh Giang Thi,

You can take a look at this article:

https://1c-dn.com/library/mobile_application_development/working_with_user­­_notifications_in_mobile_applications/

Best regards,
Vladimir Gurov

 
Subscribe
Users browsing this topic (guests: 1, registered: 0, hidden: 0)
Be the first to know tips & tricks on business application development!

A confirmation e-mail has been sent to the e-mail address you provided .

Click the link in the e-mail to confirm and activate the subscription.