iOS Question How do you send a push notification to a single iphone device

davemorris

Active Member
Licensed User
Longtime User
Hi, Guys

I am trying to send a push notification to a signal device - I have done it with an Android devices see
Push notification - Sending to one device (with the new Firebase API)

Basically I took Erel's latest push notification example and changed topic to token and provide the token/ID of the phone.

However, I tried a similar technique on the iphone and it did not work - in fact I got the error (see below).

It suggests a bad device ID but I am sure it is correct.

Can anyone help (I will post the code if it helps)?


Dave
----------
Waiting for debugger to connect...
Program started.
{
"message": {
"notification": {
"title": "Title 001",
"body": "Body 001"
},
"data": {
"title": "Title 001",
"body": "Body 001"
},
"topic": "d4ku_kuscUEJsgROTmod1Y:APA91bESjzCsRKFTX-80BItlAzmOqSVA63I9S3C7D4N9uRZ1l0wnhZ4SqjZSCbDRWQPDQSn6eWar46c6Z6-XKWhmc1HhUshqhfY3HwVZBbxZEV2hXwI082A",
"apns": {
"headers": {
"apns-priority": "10"
},
"payload": {
"aps": {
"badge": 0,
"sound": "default"
}
}
}
}
}
ResponseError. Reason: , Response: {
"error": {
"code": 400,
"message": "Request contains an invalid argument.",
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis.com/google.firebase.fcm.v1.FcmError",
"errorCode": "INVALID_ARGUMENT"
},
{
"@type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "message.token",
"description": "Invalid registration token"
}
]
}
]
}
}
----------------
 
Solution
Changing the topic with the token should work. I've just tested it.
Hi found my problem.

It was simple - When checking thro the App setup, I found did not create an Apple Push Notification Service SSL certificate for the App ID - (basically missed a stage our of the tutorial).

Sorry about that it just a case of FTBT (Follow the Bloody Tutorial).


Regards and thanks for the help

Dave Morris

davemorris

Active Member
Licensed User
Longtime User
Hi, I have just had a look at my Sending code done a few changes.

But it still does not work (I have attached my project - you will have provide your own serviceaccount.json file and target device token).
Please note the original send code (on which this code is based) - using topic works on both the Android and iOS phones.


---- the new log output is
Waiting for debugger to connect...
Program started.
{
"message": {
"notification": {
"title": "Title 001",
"body": "Body 001"
},
"data": {
"title": "Title 001",
"body": "Body 001"
},
"apns": {
"headers": {
"apns-priority": "10"
},
"payload": {
"aps": {
"badge": 0,
"sound": "default"
}
}
},
"token": "dIno-4D5rUWWrmaGjPyjve:APA91bHHWxW-SzD635rQvZrb5QqnsHzuk20qjtvYJHB21fmgBIkh-aBXk8gkLG0j2HVLjlpCvuilhsuIvNWcsww9OBdqXK13JwDVxiUNkuO-xmOBdEvgQzc"
}
}
ResponseError. Reason: , Response: {
"error": {
"code": 401,
"message": "Auth error from APNS or Web Push Service",
"status": "UNAUTHENTICATED",
"details": [
{
"@type": "type.googleapis.com/google.firebase.fcm.v1.FcmError",
"errorCode": "THIRD_PARTY_AUTH_ERROR"
}
]
}
}

Kind regards
Dave Morris
 

Attachments

  • FCMPush.zip
    2 KB · Views: 24
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Changing the topic with the token should work. I've just tested it.

B4X:
Private Sub SendMessage(Topic As String, Title As String, Body As String) As ResumableSub
    Dim Job As HttpJob
    Job.Initialize("", Me)
    Dim data As Map = CreateMap("title": Title, "body": Body)
'    Dim message As Map = CreateMap("topic": Topic, "data": data)
    Dim message As Map = CreateMap("token": DeviceToken, "data": data)
    If Topic.StartsWith("ios_") Then
        'B4i
        Dim Badge As Int = 0
        Dim iosalert As Map =  CreateMap("title": Title, "body": Body)
        message.Put("notification", iosalert)
        message.Put("apns", CreateMap("headers": _
            CreateMap("apns-priority": "10"), _
            "payload": CreateMap("aps": CreateMap("sound":"default", "badge": Badge, "category": "category 1"))))
    Else
        'B4A
        message.Put("android", CreateMap("priority": "high"))
    End If
    Dim jg As JSONGenerator
    jg.Initialize(CreateMap("message": message))
    Log(jg.ToPrettyString(4))
    Job.PostString($"https://fcm.googleapis.com/v1/projects/${ProjectId}/messages:send"$, jg.ToString)
    Job.GetRequest.SetContentType("application/json;charset=UTF-8")
    Job.GetRequest.SetHeader("Authorization", "Bearer " & Token)
    Wait For (Job) JobDone(Job As HttpJob)
    If Job.Success Then
        Log(Job.GetString)
    End If
    Job.Release
    Return True
End Sub

Do note that the tokens are refreshed quite frequently.
You can call this sub when the app starts to monitor the token:
B4X:
Private Sub UpdateToken
    Dim token As String
    Do While True
        Dim s As String = GetToken
        If s <> token Then
            token = s
            Log("token refreshed")
            Log(token)
        End If
        Sleep(3000)
    Loop
End Sub

Private Sub GetToken As String
    Dim FIRMessaging As NativeObject
    FIRMessaging = FIRMessaging.Initialize("FIRMessaging").GetField("messaging")
    Dim token As NativeObject = FIRMessaging.GetField("FCMToken")
    If token.IsInitialized = False Then Return "" Else Return token.AsString
End Sub
 
Upvote 0

davemorris

Active Member
Licensed User
Longtime User
Changing the topic with the token should work. I've just tested it.
Hi, Thanks for the input - I will check for the device Token changing.

BTW I wonder if I have not setup Firebase correctly for the iOS operation? - (but if that is wrong surely sending a topic to an iPhone would not work either).

Kind regards
Dave Morris
 
Upvote 0

davemorris

Active Member
Licensed User
Longtime User
Changing the topic with the token should work. I've just tested it.
Hi found my problem.

It was simple - When checking thro the App setup, I found did not create an Apple Push Notification Service SSL certificate for the App ID - (basically missed a stage our of the tutorial).

Sorry about that it just a case of FTBT (Follow the Bloody Tutorial).


Regards and thanks for the help

Dave Morris
 
Upvote 0
Solution
Top