Desktop version

Main > Forum > 1C:Enterprise Platform > 1C:Enterprise. Mobile platform > Code example for PushNotifications

Forum

Search UsersRules
Code example for PushNotifications
#1
Active user
Points:: 0
Joined:: Sep 1, 2014

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.

Profile
#2
Guest
Points::
Joined::

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

Profile
#3
Active user
Points:: 0
Joined:: Sep 1, 2014

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.

Profile
#4
Guest
Points::
Joined::

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

Profile
#5
Active user
Points:: 0
Joined:: Sep 1, 2014

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

Profile
#6
Guest
Points::
Joined::

Hello, Marius Gidu!

Try adding Export to the handler procedure:

Code
Procedure ReceivePushNotification(Notification, IsLocal, IsDisplayed) Export

Profile
#7
Active user
Points:: 0
Joined:: Sep 1, 2014

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?

Profile
#8
Guest
Points::
Joined::

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

Profile
#9
Active user
Points:: 0
Joined:: Sep 1, 2014

Thanks.

And code for the server?

Profile
#10
Guest
Points::
Joined::

Marius,

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

Profile
#11
Active user
Points:: 0
Joined:: Dec 4, 2015

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

Profile
#12
Active user
Points:: 0
Joined:: Jun 25, 2013

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.

Profile
#13
Active user
Points:: 0
Joined:: Dec 4, 2015

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

Profile
#14
Active user
Points:: 0
Joined:: Jun 25, 2013

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.

Profile
#15
Active user
Points:: 0
Joined:: Mar 14, 2012

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)

Profile
#16
Interested
Points:: 0
Joined:: Dec 4, 2017

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

Profile
#17
Active user
Points:: 0
Joined:: Mar 14, 2012

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)?

Profile
#18
Interested
Points:: 0
Joined:: Dec 4, 2017

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

Profile
#19
Active user
Points:: 0
Joined:: Mar 14, 2012

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!

Profile
#20
Just came
Points:: 0
Joined:: Oct 30, 2012

Hi,

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

Profile
#21
Interested
Points:: 0
Joined:: Dec 4, 2017

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

Profile
#22
Active user
Points:: 0
Joined:: Mar 14, 2012

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

Profile
#23
Interested
Points:: 0
Joined:: Dec 4, 2017

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

Profile
#24
Active user
Points:: 0
Joined:: Oct 14, 2019

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

Profile
Subscribe
Users browsing this topic (guests: 1, registered: 0, hidden: 0)



© 1C LLC. All rights reserved
1C Company respects the privacy of our customers and visitors
to our Web-site.