iOS Question Notifications with Firebase

WardOperationsLLC

Member
Licensed User
Longtime User
I swear this iOS app is going to be the end of me. I'm hitting brick wall after brick wall it seems. I cannot for the life of me get notifications to work on iOS, and I don't know where I'm going wrong. I'm over deadline on this project already and am at wits end. I managed to convince my boss that it'll be done by next week, but I've spent the last 7 hours dealing with this and have gotten no where. I beg for some guidance as this is the only functionality the app is missing other than a final polish of the GUI. Thank you all for your help in advance and I apoligize for the large and long post. If I'm missing any other information let me know and I'll post it ASAP.

Attached are screenshots of sections of the Apple Developer account stuff, and below is all the relevant sections of code. In my directory with the rest of my certificates I have the files aps.cer (the Apple Push Services certificate), ios_distribution.cer, and ios_development.cer. (See certs.png attached) I have two identifiers on the backend of the apple developer site. A wildcard called "Main Identifier" that is "com.mccoysoutdoors."* and one named "StoreApp" that is "com.mccoysoutdoors.deals". The provisioning file is iPadDevelopment.mobileprovision and a screenshot of it's properties is attached as a PNG. I have no keys setup.

I copy and pasted the code from the project in https://www.b4x.com/android/forum/threads/b4x-firebase-push-notifications-2023.148715/ to the same sections of my own app.

My GoogleService-Info.plist is in the Files/Special directory.

The code under my "Main" section is:

Main:
'Code module
#Region  Project Attributes
    #ApplicationLabel: McCoy's Outdoors Deals
    #Version: 1.0.0
    'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
    '#iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
    '#iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
    #iPhoneOrientations: Portrait
    #iPadOrientations: Portrait
    #Target: iPhone, iPad
    #ATSEnabled: True
    #MinVersion: 12
    #CertificateFile: ios_development.cer
    #Entitlement: <key>aps-environment</key><string>production</string>
    #ProvisionFile: iPadDevelopment.mobileprovision
#End Region

Sub Process_Globals
    Public App As Application
    Public NavControl As NavigationController
    Public UNC As UserNotificationCenter
End Sub

Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Dim PagesManager As B4XPagesManager
    PagesManager.Initialize(NavControl)
    App.RegisterForRemoteNotifications
    App.RegisterUserNotifications(True, True, True)
    UNC.Initialize
End Sub

Private Sub Application_RemoteNotification (Message As Map, CompletionHandler As CompletionHandler)
    FirebaseMessaging.MessageReceivedWhileInTheForeground(Message)
    CompletionHandler.Complete
End Sub

Private Sub Application_PushToken (Success As Boolean, Token() As Byte)
    Log($"PushToken: ${Success}"$)
    If Success = False Then
        Log(LastException)
    End If
End Sub

Private Sub Application_Active
    
End Sub

Private Sub Application_Background
    
End Sub

In "B4XMainPage" in Class Globals I added:

Class Globals:
    Private analytics As FirebaseAnalytics

In "B4XMainPage" in Sub B4XPage_Created I added:

Sub B4XPage_Created:
    analytics.Initialize
    Root = Root1 ' Assign the root view for the layout
        ' Load the layout from the MainPaage file
    Root.LoadLayout("iOSMain")
    CallSubDelayed2(FirebaseMessaging, "SubscribeToTopics", Array("deals"))
    #if B4A
    Wait For (CheckAndRequestNotificationPermission) Complete (HasPermission As Boolean)
    If HasPermission = False Then
        Log("no permission")
        ToastMessageShow("no permission", True)
    End If
    #Else If B4i
    Main.App.RegisterUserNotifications(True, True, True)
    Main.App.RegisterForRemoteNotifications
    #End If

And in FirebaseMessaging.bas I added one line to the MessageRecievedWhileInTheForeground Sub:

Sub B4XPage_Created:
Main.UNC.CreateNotificationWithContent(alert.Get("title"), alert.Get("body"), "identifer 2", "Category 1", 4000)


I've also attached the Firebase Messaging Campaigns section showing the attempts I've made to try to get a notification working. The different log functions show that I'm getting at least through SubscribeToTopics as I'm getting a Token returned in my logs, but no sign of any notifications (even when trying to send to that specific token.)
 

Alexander Stolte

Expert
Licensed User
Longtime User
However, the tutorial does not mention the "UserNotificationCenter" anywhere. Why are you using this? Because in the Firebase tutorial there is absolutely no mention of this class, let alone in the example project.

It is important that you use the B4J Sending tool to send the push messages. If you send a push via the Firebase console, it will not arrive in B4A or B4I.
 
Upvote 0

WardOperationsLLC

Member
Licensed User
Longtime User
However, the tutorial does not mention the "UserNotificationCenter" anywhere. Why are you using this? Because in the Firebase tutorial there is absolutely no mention of this class, let alone in the example project.

It is important that you use the B4J Sending tool to send the push messages. If you send a push via the Firebase console, it will not arrive in B4A or B4I.

The User notification center was for showing notification when the app was in the foreground. It failed to work before that was added, however I didn't realize sending from console wouldn't work. I'll configure and try the B4J sending tool tomorrow at work. Thanks for the reply, I'll update when I know something
 
Upvote 0

WardOperationsLLC

Member
Licensed User
Longtime User
However, the tutorial does not mention the "UserNotificationCenter" anywhere. Why are you using this? Because in the Firebase tutorial there is absolutely no mention of this class, let alone in the example project.

It is important that you use the B4J Sending tool to send the push messages. If you send a push via the Firebase console, it will not arrive in B4A or B4I.

Removed all references to the "UserNotificationCenter", downloaded the sending tool, and configured it following the steps provided in the tutorial. Sent a notification using the sending tool and didn't recieve it. Do you happen to know how to check that the B4J sending tool actually sent it to Firebase so I can verify I at least have that part working?
 
Upvote 0

WardOperationsLLC

Member
Licensed User
Longtime User
I am well and truly stupid. There was a short between the keyboard and chair. It was an ID10T error. I managed to get the example working without issue, then went to troubleshoot my code. Below is the old code and the new code that fixed it. Anyone see what I did? I called the SubscribeToTopics code before I registered for notifications. Now if you will excuse me I'm going to turn in my developer card and start looking for work elsewhere.

Old Code:
Private Sub B4XPage_Created (Root1 As B4XView)
 B4XPages.GetManager.LogEvents=True
    analytics.Initialize
    Root = Root1 ' Assign the root view for the layout
        ' Load the layout from the MainPaage file
    Root.LoadLayout("iOSMain")
    CallSubDelayed2(FirebaseMessaging, "SubscribeToTopics", Array("general"))
        #if B4A
    Wait For (CheckAndRequestNotificationPermission) Complete (HasPermission As Boolean)
    If HasPermission = False Then
        Log("no permission")
        ToastMessageShow("no permission", True)
    End If
    #Else If B4i
    Main.App.RegisterUserNotifications(True, True, True)
    Main.App.RegisterForRemoteNotifications
    #End If

New Code:
Private Sub B4XPage_Created (Root1 As B4XView)
    #if B4A
    Wait For (CheckAndRequestNotificationPermission) Complete (HasPermission As Boolean)
    If HasPermission = False Then
        Log("no permission")
        ToastMessageShow("no permission", True)
    End If
    #Else If B4i
    Main.App.RegisterUserNotifications(True, True, True)
    Main.App.RegisterForRemoteNotifications
    #End If
    B4XPages.GetManager.LogEvents=True
    analytics.Initialize
    Root = Root1 ' Assign the root view for the layout
        ' Load the layout from the MainPaage file
    Root.LoadLayout("iOSMain")
    CallSubDelayed2(FirebaseMessaging, "SubscribeToTopics", Array("general"))
 
Upvote 0
Top