PushNotificationPlugin icon indicating copy to clipboard operation
PushNotificationPlugin copied to clipboard

Android: Notification not working when app killed from background and again open

Open fazil1979 opened this issue 5 years ago • 2 comments

It works like this:

  • it works if app is in foreground or background,
  • if kill app and then send notification it not works
  • if you again open app and it's again either in background or foreground notifications not come

fazil1979 avatar Nov 07 '20 13:11 fazil1979

Try doing the solution here #33

An update:

Plugin.PushNotification 3.3.3 Xamarin.Firebase.Messaging 120.1.7 Xamarin.GooglePlayServices.Basement 117.2.1 Xamarin.Google.PlayServices.Tasks 117.2.1

Manifest:

<uses-permission android:name="android.permission.WAKE_LOCK" />
   <uses-permission android:name="android.permission.INTERNET" />
   <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" />
    <meta-data android:name="firebase_analytics_collection_enabled" android:value="false" />
    <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND">
      <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
        <category android:name="{packageName}" />
      </intent-filter>
    </receiver>

Application Class:

public override void OnCreate()
        {
            base.OnCreate();
            if (Build.VERSION.SdkInt < BuildVersionCodes.O)
            {
                return;
            }
            PushNotificationManager.DefaultNotificationChannelImportance = NotificationImportance.High;
            PushNotificationManager.Initialize(this, resetToken: true);
        }

Initialisation is made on Application Class Close App: Send first notification = Success Succeeding notifications = Fail

Open App: Send notification = Fail

Close the app and open it again, notifications are received.

This is a weird behaviour.

jbtdevgit avatar Nov 18 '20 00:11 jbtdevgit

I've managed to track down the bug, but need confirmation to the plugin owners.

During debug the variable reset token invalidates the registration whenever you kill the app, so you have to open/close/open again for the notification works. PushNotificationManager.Initialize(this, new yourCustomPushHandler(), resetToken: true);

Error in. azure when resetToken is true after closing the application:

The Notification was successfully sent to the Push Notification System
gcm
The Push Notification System handle for the registration is no longer valid

This can be reproduced easily with a blank application along with azure hub notifications.

So the fix is to set the reset token to false or create a configuration where you should apply the resetToken to true or false.

jbtdevgit avatar Nov 19 '20 08:11 jbtdevgit