10

I am implementing FCM push notifications using FirebasePushNotificationPlugin in Xamarin.Forms. In iOS project, in AppDelegate when RegisteredForRemoteNotifications method calling deviceToken generating but when I am sending notification for generated token by Postman I am getting error.

{ "multicast_id": 8631208504861228784, "success": 0, "failure": 1, "canonical_ids": 0, "results": [ { "error": "InvalidRegistration" } ] }

This is the code I have on AppDelegate got from here:

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    global::Xamarin.Forms.Forms.Init();
    LoadApplication(new App());

    FirebasePushNotificationManager.Initialize(options, new NotificationUserCategory[]
    {
        new NotificationUserCategory("message",new List<NotificationUserAction> {
            new NotificationUserAction("Reply","Reply",NotificationActionType.Foreground)
        }),
        new NotificationUserCategory("request",new List<NotificationUserAction> {
            new NotificationUserAction("Accept","Accept"),
            new NotificationUserAction("Reject","Reject",NotificationActionType.Destructive)
        })
    });

    return base.FinishedLaunching(app, options);
}

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
    FirebasePushNotificationManager.DidRegisterRemoteNotifications(deviceToken);
    Console.WriteLine("Token- - - :  "+deviceToken);
}

public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
{
    FirebasePushNotificationManager.RemoteNotificationRegistrationFailed(error);
}

public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
    FirebasePushNotificationManager.DidReceiveMessage(userInfo);
    System.Console.WriteLine(userInfo);
    completionHandler(UIBackgroundFetchResult.NewData);
}

data object in Postman when sending sample notification

{ 
 "to":"79f64b43339859a329a935f7a3e417ecc1599fbb5d6935afbooa3b4291c07fa7", 
 "notification" : {
 "body" : "New task",
 "content_available" : true,
 "priority" : "high",
 "color":"Page1",
 "title":"Announcement"
 },
 "data" : {
 "color":"Page1",
 "title":"title",
 "content_available" : true,
 "body" : "New Announcement ad"

}
}

Body of Postman

enter image description here

These are the Provisioning profiles setting from Visual Studio

enter image description here

How can I solve this issue?

R15
  • 13,982
  • 14
  • 97
  • 173
  • 1
    In iOS Speaking, it's a `(NS)Data` object. Usually, we translated it "as such" into a String Hex for our back end (the last combination should be the one to work). – Larme Apr 09 '19 at 14:31
  • @Larme - Thank you for your input. – R15 Apr 09 '19 at 15:04
  • Did you create everything on FCM properly? You have uploaded push certificates created in Apple Developer? – Ivan Ičin May 23 '19 at 10:00

2 Answers2

5

I'm not familiar with Xamarin. But I worked with FCM a lot.

I think you are getting wrong token. Using deviceToken won't work with push notification from FCM. I did a search and maybe you have to get it from

var fcmToken = FirebaseInstanceId.Instance.Token;

More detail: https://learn.microsoft.com/en-us/xamarin/android/data-cloud/google-messaging/remote-notifications-with-fcm?tabs=macos

tuledev
  • 10,177
  • 4
  • 29
  • 49
  • @Arvindraja It means the token for `to` client is right. And you're having another issue, maybe your auth is wrong https://stackoverflow.com/questions/37863106/fcm-getting-mismatchsenderid – tuledev May 28 '19 at 07:37
  • Last three days I have tried everything but not getting first notificN. Sender Id is same on portal and in GoogleService file. – R15 May 28 '19 at 07:41
  • @Arvindraja 2 The MismatchSenderId error is encountered when you are attempting to send to a registration token that is associated with a different Sender (Project). Please check your Authorization key (in Postman) and Project ID(where you got fcm token from) – tuledev May 28 '19 at 07:49
  • I have two projects with same bundleID. First is working as usual. When I am downloading new GoogleJson file and adding to 2nd project and getting FCM token while debugging. To that token notification not getting sent. Authorization key is always one which I am using from Firebase portal. I have only one project on firebase. – R15 May 28 '19 at 07:54
  • @Arvindraja Does token device from 1st project work with Postman (with same Auth key)? – tuledev May 28 '19 at 08:01
  • @Arvindraja Wait... I see your Authorization key in Postman is `Alza.....` It's wrong. Use the key like `AAAA....`. Check this https://stackoverflow.com/questions/37863106/fcm-getting-mismatchsenderid/42290231#42290231 – tuledev May 28 '19 at 08:05
  • @Arvindraja You have to do 2 things: 1. Get fcm token as my answer. 2. Change your Auth key to `AAAA....`. Then Postman will work – tuledev May 28 '19 at 08:07
  • first project is working with Postman with the same keys. But second not working. I checked both keys, Server key and Legacy server key are same it doesn't make any diff. However I tried both but not working for second project. So can u please help me regarding this. – R15 May 28 '19 at 10:06
  • @Arvindraja So weird. Can you please try to copy Google..json from 1st project to 2nd instead of download new one?. – tuledev May 28 '19 at 10:09
  • I got it work. I have one more googleJson file added in Resource folder which had different SenderId so I was getting this error. Now working everything. – R15 May 30 '19 at 12:38
  • @Arvindraja Nice!!!. What did you change? Just changed the googleJson? Did you use `FirebaseInstanceId.Instance.Token` or just `deviceToken`? – tuledev May 30 '19 at 16:47
  • 1
    I have been using `Instance.Token` not `deviceToken`. But due to hidden GoogleJson file, notification was not working. When you answered this question just one day before I came to know about `Instance.Token`. As per question your answer is correct, SenderId issue was additional. Can you have look on [this](https://learn.microsoft.com/en-us/xamarin/ios/platform/user-notifications/deprecated/remote-notifications-in-ios) and search for _DeviceToken_ – R15 May 30 '19 at 17:20
0

Edited because my initial answer regarding RegisteredForRemoteNotifications was wrong

As documented here, the actual token is in the Description of the token received:

public override void RegisteredForRemoteNotifications (
UIApplication application, NSData deviceToken)
{
    // Get current device token
    var DeviceToken = deviceToken.Description;
    if (!string.IsNullOrWhiteSpace(DeviceToken)) {
        DeviceToken = DeviceToken.Trim('<').Trim('>');
    }

    // Get previous device token
    var oldDeviceToken = NSUserDefaults.StandardUserDefaults.StringForKey("PushDeviceToken");

    // Has the token changed?
    if (string.IsNullOrEmpty(oldDeviceToken) || !oldDeviceToken.Equals(DeviceToken))
    {
        //TODO: Put your own logic here to notify your server that the device token has changed/been created!
    }

    // Save new device token
    NSUserDefaults.StandardUserDefaults.SetString(DeviceToken, "PushDeviceToken");
}

So the token is DeviceToken above.

As an alternative to implementing _ RegisteredForRemoteNotifications_, you can do the following:

  1. Make your AppDelegate implement the IMessagingDelegate interface.
  2. Implement the following method:

    // This callback is fired whenever a new token is generated - in order for it to be called, the AppDelegate must be an IMessagingDelegate

    [Export("messaging:didReceiveRegistrationToken:")]

    public async void DidReceiveRegistrationToken(Messaging messaging, string token) {

            // Subscribe to a 'news' topic so we can send to just those subscribed to this topic
            messaging.Subscribe("news");
    
    
            // Log this to debug output so that we can capture it for testing
            Debug.WriteLine($"DidReceiveRegistration Token:'{token}'");
    
    
        }
    
Bhavik Modi
  • 1,517
  • 14
  • 29
James Lavery
  • 920
  • 4
  • 25
  • Not working this at all. This already I have been following..If you read my question properly. – R15 Apr 10 '19 at 11:36
  • Sorry - my code for the DidReceiveRegistrationToken was wrong. I've corrected it. I suggest you try that approach. – James Lavery Apr 10 '19 at 12:03