iOS Question Push Notifications RemoteNotification event never runs

Hccsoft

Member
Licensed User
After compiling the push server, executing it from the windows cmd, and after sending a message from b4i with the "send" handle (Even when I debug push server I’ve got no error log):

cmd.png



After sending the token and then a message with the "send" handle, I get this in the b4i log:

upload_2018-1-15_18-40-38.png


These are the certificate, and provision file:

upload_2018-1-15_18-41-10.png


the fact is that I can not detect any errors but the debugger never stops in the Application_RemoteNotification event

these are the settings for notifications of the app:

notif.jpg




And this is the b4i code, can you tell me what I'm missing?

B4X:
'Code module
#Region  Project Attributes
   #ApplicationLabel: B4i Example
   #Version: 1.0.0
   #Entitlement: <key>aps-environment</key><string>production</string>
   'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
   #iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
   #iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
   #ProvisionFile: Push.mobileprovision
   '#PlistExtra: <key>UIBackgroundModes</key><array><string>remote-notification</string></array>
#End Region

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'Public variables can be accessed from all modules.
   Public App As Application
   Public NavControl As NavigationController
   Private Page1 As Page
   Private Const ServerUrl As String = "http://192.168.1.40:51044"

End Sub

Private Sub Application_Start (Nav As NavigationController)
   NavControl = Nav
   Page1.Initialize("Page1")
   Page1.Title = "Page 1"
   Page1.RootPanel.Color = Colors.White
   Dim but As Button
   but.Initialize("but",but.STYLE_SYSTEM)
   but.Text ="SEND PUSH"
   Page1.RootPanel.AddView(but,100,100,100,20)
   NavControl.ShowPage(Page1)
   App.RegisterUserNotifications(True, True, True)
   App.RegisterForRemoteNotifications
End Sub

Sub but_Click
   Dim job As HttpJob
   job.Initialize("jobsend", Me)
   'job.PostString(ServerUrl & "/send", "password=123" & "&text=Hello" & "&badge=1" & "&sound=piece.mp3")
   job.PostString(ServerUrl & "/send", "password=123" & "&text=Hello" & "&badge=1")
End Sub


Private Sub Application_PushToken (Success As Boolean, Token() As Byte)
   If Success Then
       Dim bc As ByteConverter
       Dim j As HttpJob
       j.Initialize("j", Me)
       j.PostString(ServerUrl & "/devicetoken", "token=" & bc.HexFromBytes(Token) & "&type=1")
   Else
       Log("Error getting token: " & LastException)
   End If
End Sub

Private Sub JobDone(j As HttpJob)
   If j.Success Then
       Log("Token uploaded successfully.")
   Else
       Log("Error uploading token")
   End If
   j.Release
End Sub

Private Sub Application_RemoteNotification (Message As Map, CompletionHandler As CompletionHandler)
   Log("Remote notification: " & Message)
   Dim m As Map = Message.Get("aps")
   Log(m)
   Log(m.Get("alert"))
   CompletionHandler.Complete
End Sub

'Private Sub Application_RemoteNotification (Message As Map)
'   Log("Remote notification: " & Message)
'   Dim m As Map = Message.Get("aps")
'   Log(m)
'   Log(m.Get("alert"))
'End Sub



Private Sub Page1_Resize(Width As Int, Height As Int)
 
End Sub

Private Sub Application_Background
 
End Sub
 

Attachments

  • upload_2018-1-15_18-40-14.png
    upload_2018-1-15_18-40-14.png
    69.4 KB · Views: 162
Last edited:

Hccsoft

Member
Licensed User
I've done it with developer certificates, and now the debugger stops at Application_RemoteNotification, but the notifications still do not appear.
However, the iphone receive notifications from other app, do I miss something?

b4i.jpg


After compiling the push server, executing it from the windows cmd, and after sending a message from b4i with the "send" handle (Even when I debug push server I’ve got no error log):

View attachment 63723


After sending the token and then a message with the "send" handle, I get this in the b4i log:

View attachment 63699

These are the certificate, and provision file:

View attachment 63700

the fact is that I can not detect any errors but the debugger never stops in the Application_RemoteNotification event

these are the settings for notifications of the app:

View attachment 63727



And this is the b4i code, can you tell me what I'm missing?

B4X:
'Code module
#Region  Project Attributes
   #ApplicationLabel: B4i Example
   #Version: 1.0.0
   #Entitlement: <key>aps-environment</key><string>production</string>
   'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
   #iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
   #iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
   #ProvisionFile: Push.mobileprovision
   '#PlistExtra: <key>UIBackgroundModes</key><array><string>remote-notification</string></array>
#End Region

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'Public variables can be accessed from all modules.
   Public App As Application
   Public NavControl As NavigationController
   Private Page1 As Page
   Private Const ServerUrl As String = "http://192.168.1.40:51044"

End Sub

Private Sub Application_Start (Nav As NavigationController)
   NavControl = Nav
   Page1.Initialize("Page1")
   Page1.Title = "Page 1"
   Page1.RootPanel.Color = Colors.White
   Dim but As Button
   but.Initialize("but",but.STYLE_SYSTEM)
   but.Text ="SEND PUSH"
   Page1.RootPanel.AddView(but,100,100,100,20)
   NavControl.ShowPage(Page1)
   App.RegisterUserNotifications(True, True, True)
   App.RegisterForRemoteNotifications
End Sub

Sub but_Click
   Dim job As HttpJob
   job.Initialize("jobsend", Me)
   'job.PostString(ServerUrl & "/send", "password=123" & "&text=Hello" & "&badge=1" & "&sound=piece.mp3")
   job.PostString(ServerUrl & "/send", "password=123" & "&text=Hello" & "&badge=1")
End Sub


Private Sub Application_PushToken (Success As Boolean, Token() As Byte)
   If Success Then
       Dim bc As ByteConverter
       Dim j As HttpJob
       j.Initialize("j", Me)
       j.PostString(ServerUrl & "/devicetoken", "token=" & bc.HexFromBytes(Token) & "&type=1")
   Else
       Log("Error getting token: " & LastException)
   End If
End Sub

Private Sub JobDone(j As HttpJob)
   If j.Success Then
       Log("Token uploaded successfully.")
   Else
       Log("Error uploading token")
   End If
   j.Release
End Sub

Private Sub Application_RemoteNotification (Message As Map, CompletionHandler As CompletionHandler)
   Log("Remote notification: " & Message)
   Dim m As Map = Message.Get("aps")
   Log(m)
   Log(m.Get("alert"))
   CompletionHandler.Complete
End Sub

'Private Sub Application_RemoteNotification (Message As Map)
'   Log("Remote notification: " & Message)
'   Dim m As Map = Message.Get("aps")
'   Log(m)
'   Log(m.Get("alert"))
'End Sub



Private Sub Page1_Resize(Width As Int, Height As Int)
 
End Sub

Private Sub Application_Background
 
End Sub
After compiling the push server, executing it from the windows cmd, and after sending a message from b4i with the "send" handle (Even when I debug push server I’ve got no error log):

View attachment 63723


After sending the token and then a message with the "send" handle, I get this in the b4i log:

View attachment 63699

These are the certificate, and provision file:

View attachment 63700

the fact is that I can not detect any errors but the debugger never stops in the Application_RemoteNotification event

these are the settings for notifications of the app:

View attachment 63727



And this is the b4i code, can you tell me what I'm missing?

B4X:
'Code module
#Region  Project Attributes
   #ApplicationLabel: B4i Example
   #Version: 1.0.0
   #Entitlement: <key>aps-environment</key><string>production</string>
   'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
   #iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
   #iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
   #ProvisionFile: Push.mobileprovision
   '#PlistExtra: <key>UIBackgroundModes</key><array><string>remote-notification</string></array>
#End Region

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'Public variables can be accessed from all modules.
   Public App As Application
   Public NavControl As NavigationController
   Private Page1 As Page
   Private Const ServerUrl As String = "http://192.168.1.40:51044"

End Sub

Private Sub Application_Start (Nav As NavigationController)
   NavControl = Nav
   Page1.Initialize("Page1")
   Page1.Title = "Page 1"
   Page1.RootPanel.Color = Colors.White
   Dim but As Button
   but.Initialize("but",but.STYLE_SYSTEM)
   but.Text ="SEND PUSH"
   Page1.RootPanel.AddView(but,100,100,100,20)
   NavControl.ShowPage(Page1)
   App.RegisterUserNotifications(True, True, True)
   App.RegisterForRemoteNotifications
End Sub

Sub but_Click
   Dim job As HttpJob
   job.Initialize("jobsend", Me)
   'job.PostString(ServerUrl & "/send", "password=123" & "&text=Hello" & "&badge=1" & "&sound=piece.mp3")
   job.PostString(ServerUrl & "/send", "password=123" & "&text=Hello" & "&badge=1")
End Sub


Private Sub Application_PushToken (Success As Boolean, Token() As Byte)
   If Success Then
       Dim bc As ByteConverter
       Dim j As HttpJob
       j.Initialize("j", Me)
       j.PostString(ServerUrl & "/devicetoken", "token=" & bc.HexFromBytes(Token) & "&type=1")
   Else
       Log("Error getting token: " & LastException)
   End If
End Sub

Private Sub JobDone(j As HttpJob)
   If j.Success Then
       Log("Token uploaded successfully.")
   Else
       Log("Error uploading token")
   End If
   j.Release
End Sub

Private Sub Application_RemoteNotification (Message As Map, CompletionHandler As CompletionHandler)
   Log("Remote notification: " & Message)
   Dim m As Map = Message.Get("aps")
   Log(m)
   Log(m.Get("alert"))
   CompletionHandler.Complete
End Sub

'Private Sub Application_RemoteNotification (Message As Map)
'   Log("Remote notification: " & Message)
'   Dim m As Map = Message.Get("aps")
'   Log(m)
'   Log(m.Get("alert"))
'End Sub



Private Sub Page1_Resize(Width As Int, Height As Int)
 
End Sub

Private Sub Application_Background
 
End Sub
 
Last edited:
Upvote 0

Hccsoft

Member
Licensed User
It will never pause in Application_Background.

You can run the app in the background for a minute or two. It will then be killed.

So, debuging can't I execute the app in background? I did it and the notifications don't appear
 
Upvote 0

Hccsoft

Member
Licensed User
Your app is killed about 30 seconds after it moves to the background. This is how iOS works.

What is the question?

You don't see the notification when your app is not running?
Are you sending the messages with the B4J code?

I send message from b4i from a button with
job.PostString(ServerUrl & "/send", "password=123" & "&text=Hello" & "&badge=1")
as you can see in the code, then b4j push server sends it to b4i, so (only with developper certificates) i receive it in Application_RemoteNotification.
Then I click home iphone button, so app is then in background, isn't it? I've also opened another app, but notifications don't show.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
1. Why are you using push server and not Firebase Push? Firebase Push is simpler and better.
2. Don't use development certificates at all. Only production certificate.
3. Make sure to add the entitlement line:
B4X:
#Entitlement: <key>aps-environment</key><string>production</string>
Should be development instead of production if you are using a development certificate.
4. Have you allowed your app to show notifications?
 
Upvote 0

Hccsoft

Member
Licensed User
1. Why are you using push server and not Firebase Push? Firebase Push is simpler and better.
2. Don't use development certificates at all. Only production certificate.
3. Make sure to add the entitlement line:
B4X:
#Entitlement: <key>aps-environment</key><string>production</string>
Should be development instead of production if you are using a development certificate.
4. Have you allowed your app to show notifications?
1. Why are you using push server and not Firebase Push? Firebase Push is simpler and better.
2. Don't use development certificates at all. Only production certificate.
3. Make sure to add the entitlement line:
B4X:
#Entitlement: <key>aps-environment</key><string>production</string>
Should be development instead of production if you are using a development certificate.
4. Have you allowed your app to show notifications?
Yes, In the first post you can see the configuration of the notifications of the app. The problem is that with distribution certificates (with the production entitlement) the Application_RemoteNotification event never run, as you can see in the first post. Is there anything else to add for production certificates?. If not, maybe I'll do it whith firebase as you say.
 
Upvote 0

Hccsoft

Member
Licensed User
Hello Erel, after switching from b4j push server to firebase, de debugger finally stops in Application_Remotenotification with distribution certifictates. When I click home button the log shows that the app goes through Application_Inactive and Application_Background events, but I never receive the notification. These are the logs, and at the end I’ve copied b4i and b4j (sending tool) code.


B4I LOG:

After uninstalling app

Application_Start
Configuring the default app.
Application_Active
Application_Pushtoken
PushToken: 1
<B4IExceptionWrapper: (null)>
Application_Pushtoken
PushToken: 1
<B4IExceptionWrapper: (null)>
FCMConnected



non-ui B4J program:


Waiting for debugger to connect...
Program started.
[jobname=fcm, success=true, username=
, password=, errormessage=, target=class b4j.example.main
, taskid=1, req=anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpRequest@10b95f2, tag=java.lang.Object@1a1446d
, httputils2service=null]
{"message_id":8510241710889756717}

The bundle id in firebase is the same as the one of the app id, and the one in project compiler Settings. The api key I use for cloud messaging in the b4j program is the server key:




I copied GoogleService-Info.plist into Files/Special folder and I uploaded firebase_push.p12 file to apns production certificate in firebase.


This is the code, do I miss something?:


B4I:

B4X:
'Code module
#Region  Project Attributes
   #ApplicationLabel: B4i Example
   #Version: 1.0.0
   #Entitlement: <key>aps-environment</key><string>production</string>

   'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
   #iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
   #iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
 
   #CertificateFile: ios_distribution.cer
    'use the provision profile that goes with the explicit App Id
   #ProvisionFile: Push.mobileprovision
</array>
#End Region

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'Public variables can be accessed from all modules.
   Public App As Application'
   Public NavControl As NavigationController
 
   Private Page1 As Page
   Private analytics As FirebaseAnalytics
   Private fm As FirebaseMessaging
   Private hd As HUD
End Sub

Private Sub Application_Start (Nav As NavigationController)
   analytics.Initialize
   NavControl = Nav
   Page1.Initialize("Page1")
   Page1.Title = "Page 1"
   Page1.RootPanel.Color = Colors.White
   NavControl.ShowPage(Page1)
   App.RegisterUserNotifications(True, True, True)
   App.RegisterForRemoteNotifications
   fm.Initialize("fm")
End Sub


Private Sub fm_FCMConnected
   Log("FCMConnected")
   'here we can subscribe and unsubscribe from topics
   fm.SubscribeToTopic("ios_general") 'add ios_ prefix to all topics
End Sub


Private Sub Application_PushToken (Success As Boolean, Token() As Byte)
   Log($"PushToken: ${Success}"$)
   'Log($"token: "${GetToken}"$)
   Log(LastException)
End Sub


Private Sub GetToken As String
   Dim no As NativeObject
   Dim token As NativeObject = no.Initialize("FIRInstanceID").RunMethod("instanceID", Null).RunMethod("token", Null)
   If token.IsInitialized Then Return token.AsString Else Return ""
End Sub

Private Sub JobDone(j As HttpJob)
   If j.Success Then
       Log("Token uploaded successfully.")
   Else
       Log("Error uploading token")
   End If
   j.Release
End Sub

Private Sub Application_RemoteNotification (Message As Map, CompletionHandler As CompletionHandler)
   hd.ToastMessageShow("remotenotification",False)
   Log("Remote notification: " & Message)
   Dim m As Map = Message.Get("aps")
   Log(m)
   Log(m.Get("alert"))
   CompletionHandler.Complete
End Sub


Private Sub Page1_Resize(Width As Int, Height As Int)
 
End Sub

Private Sub Application_Active
   fm.FCMConnect 'should be called from Application_Active
End Sub

Private Sub Application_Background
   fm.FCMDisconnect 'should be called from Application_Background
End Sub


B4J Sending tool:

B4X:
'Non-UI application (console / server application)
#Region  Project Attributes
   #CommandLineArgs:
   #MergeLibraries: True
#End Region

Sub Process_Globals
   'Private const API_KEY As String = "AIzxxxxxxxxxxxxxxxx"
    Private const API_KEY As String = "AAAANskAc6I:APA91b....."
 
End Sub

Sub AppStart (Args() As String)
   SendMessage("ios_general", "title", "body")
   StartMessageLoop
End Sub

Private Sub SendMessage(Topic As String, Title As String, Body As String)
   Dim Job As HttpJob
   Job.Initialize("fcm", Me)
   Dim m As Map = CreateMap("to": $"/topics/${Topic}"$)
   Dim data As Map = CreateMap("title": Title, "body": Body)
   If Topic.StartsWith("ios_") Then
       Dim iosalert As Map =  CreateMap("title": Title, "body": Body, "sound": "default")
       m.Put("notification", iosalert)
       m.Put("priority", 10)
   End If
   m.Put("data", data)
   Dim jg As JSONGenerator
   jg.Initialize(m)
   Job.PostString("https://fcm.googleapis.com/fcm/send", jg.ToString)
   Job.GetRequest.SetContentType("application/json;charset=UTF-8")
   Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)
End Sub


Sub JobDone(job As HttpJob)
   Log(job)
   If job.Success Then
     Log(job.GetString)
   End If
   job.Release
   ExitApplication '!
End Sub
 

Attachments

  • firebase.jpg
    firebase.jpg
    25.7 KB · Views: 172
Last edited:
Upvote 0

Hccsoft

Member
Licensed User
The images are broken.

Please post the logs as text, not screenshots.

No logs should appear when your app is in the background and it receives a notification message. The notification itself should appear.

Sorry, i've changed the images for text. I've only executed the app from compiled file (without debuging), and even compiled the sending tool, but when I put my app on background mode the notification still don't appear. Am I missing anything in the code?
 
Upvote 0

Hccsoft

Member
Licensed User
You do receive the message if the app is in the foreground, right (in the logs only)?

Yes, this is what appears in the log:

Application_Start
Configuring the default app.
Application_Active
FCMConnected
Application_Inactive
Application_Active
Application_Pushtoken
PushToken: 1
<B4IExceptionWrapper: (null)>
FCMConnected
Application_Remotenotification
Remote notification: (read only map) {
aps = "(read only map) {\n alert = \"(read only map) {\\n body = body;\\n title = title;\\n}\";\n sound = default;\n}";
body = body;
"gcm.message_id" = "0:1516291735741563%ef0e0ef6ef0e0ef6";
title = title;
}
(read only map) {
alert = "(read only map) {\n body = body;\n title = title;\n}";
sound = default;
}
(read only map) {
body = body;
title = title;
}
 
Upvote 0

Hccsoft

Member
Licensed User
Were you asked to allow notifications when you first run you program?
B4X:
 App.RegisterUserNotifications(True, True, True)
   App.RegisterForRemoteNotifications

here, in application_start:
B4X:
Private Sub Application_Start (Nav As NavigationController)
   analytics.Initialize
   NavControl = Nav
   Page1.Initialize("Page1")
   Page1.Title = "Page 1"
   Page1.RootPanel.Color = Colors.White
   NavControl.ShowPage(Page1)
   App.RegisterUserNotifications(True, True, True)
   App.RegisterForRemoteNotifications
   fm.Initialize("fm")
End Sub
 
Upvote 0
Top