0

Hello I am trying tyo implement FCM in xamarin forms app. i Installed Plugin.FirebasePushNotification Plugin

i implement refresh token class

[Service]
[IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
public class MyFirebaseIIDService : FirebaseInstanceIdService
{
    const string TAG = "MyFirebaseIIDService";
    public override void OnTokenRefresh()
    {
        var refreshedToken = FirebaseInstanceId.Instance.Token;
        Log.Debug(TAG, "Refreshed token: " + refreshedToken);
        SendRegistrationToServer(refreshedToken);
    }
    void SendRegistrationToServer(string token)
    {
        // Add custom implementation, as needed.
    }
}

and this class for receive notification but not get notification when app is close

  [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
        public class MyFirebaseMessagingService : FirebaseMessagingService
        {
            const string TAG = "MyFirebaseMsgService";

            // [START receive_message]
            public override void OnMessageReceived(RemoteMessage message)
            {
        }
    }

and i used this payload

{ "to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...", "notification" : { "body" : "great match!", "title" : "Portugal vs. Denmark" } }

Deepak yogi
  • 107
  • 1
  • 14
  • 1
    If your app is in debug mode you need to reopen once & close. Than try sending notification. – R15 Jan 25 '19 at 13:18
  • @CGPA6.4 notification receive when app open if closed not received – Deepak yogi Jan 25 '19 at 13:22
  • Is your app being developed in debug mode? if yes than after opening app, once again & close it. Than send notifications. – R15 Jan 25 '19 at 13:24
  • @CGPA6.4 not work – Deepak yogi Jan 25 '19 at 13:34
  • You can try to sent `data` in place of `notification` in payload & check. – R15 Jan 25 '19 at 13:34
  • Run your application in release mode and then try to push notification. – Junior Jiang Jan 28 '19 at 08:26
  • @JuniorJiang-MSFT app closed the then not receive notification in release mode – Deepak yogi Jan 29 '19 at 06:35
  • Hello @JuniorJiang-MSFT i used this payload `{ "to" : "token", "data" : { "body" : "data match!", "content_available" : true, "priority" : "high", "title" : "Portugal vs. Denmark" } }` work perfect but if app initialize and close app, then send notification from postman notification not receive. if one time open app and after close app then send notification from postman, notification received. – Deepak yogi Jan 29 '19 at 07:58
  • If you want to handle notification when the application is backgrounded you should send `data message` and use `onMessageReceived` method.Changing `{ "to" : "token", "notification" : {..}` to `{ "to" : "token", "data" : {..}` .Not using notification . – Junior Jiang Jan 30 '19 at 03:18
  • Your problem maybe not the type of notification.Later I will update answer. – Junior Jiang Jan 30 '19 at 06:06

2 Answers2

1

From Android Official document,FirebaseMessaging has method of subscribeToTopic .

public Task subscribeToTopic (String topic)

Subscribes to topic in the background. The subscribe operation is persisted and will be retried until successful. This uses a Firebase Instance ID token to identify the app instance and periodically sends data to the Firebase backend.

Here is a official sample for Android FCM Remote Notifications.After testing this project,If not subscribing notification, notification will not receive when app stopped.

You can have a try.Code as follow when App load:

FirebaseMessaging.Instance.SubscribeToTopic("news");
Junior Jiang
  • 12,430
  • 1
  • 10
  • 30
  • Hello @Junior i received notification via Refresh token not topic so any solution for refresh token – Deepak yogi Jan 31 '19 at 08:23
  • Do you mean that not receiving in `OnTokenRefresh`?Maybe this is another question.If answer be helpful,you can mark it accepted to close question and reopen another question about `Refresh token`.^.^ – Junior Jiang Jan 31 '19 at 08:44
  • If want `OnTokenRefresh` getting called first need `MyFirebaseIIDService` be called.Here is a good discussion about this.(https://stackoverflow.com/questions/46770907/ontokenrefresh-never-called-in-xamarin-android) – Junior Jiang Jan 31 '19 at 08:50
  • Hello @Junior i receive notification in release and debug mode and background and foreground, but if i debug application then close debug and send notification, notification not receive after click app icon and open app then close app and send notification notification received **Conclusion**: Debug app close debug, close app, then send notification, notification not receive after then open app and close app, send notification, notification received. – Deepak yogi Jan 31 '19 at 11:24
  • When click `closing debug` ,this killing the `PID` of app.Then will not receivce notification,service of app are not runing.Suggest that testing notification using the normal interface action to click to close the app, so you can receive notifications, if you have already subscribed. – Junior Jiang Feb 01 '19 at 02:47
0

In my case. I added OneTimeWork with Result.retry and added setBackoffCriteria to 30 seconds to ensure FirebaseMessaging.getInstance().isAutoInitEnabled().

If the result is false, I run it again with FirebaseMessaging.getInstance(). setAutoInitEnabled(true).

Sorry if my english is so bad, because I use Google Translate.

Obsidian
  • 3,719
  • 8
  • 17
  • 30