FCM - Not Receiving Notifications when the app is

2020-08-03 04:01发布

I migrated to Firebase Cloud Messaging for push notifications. Every thing works great , I get notifications whenever the app is open or running in background , but when the app is closed and a message is sent from the server then you don't receive it in the device ( obviously ). Now when I reopen the app , I should get the pending notification ( the notification sent while the app was closed ) but I don't get the notification.

This is my FireBaseMessagingService class

public class MyFirebaseMessagingService extends FirebaseMessagingService {
public static String TAG="Firebase";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.d(TAG, "From: " + remoteMessage.getFrom());
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getData().get("message"));
    notification(remoteMessage.getData().get("message"));
}
void notification(String s)
{
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
    mBuilder.setSmallIcon(R.mipmap.logo);
    mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
    mBuilder.setContentTitle("App");
    mBuilder.setContentText(s);
    mBuilder.setAutoCancel(true);
    Intent resultIntent = new Intent(this, MainActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    mNotificationManager.notify(101,mBuilder.build());
}
}

This is my FirebaseInstance class

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
public static String TAG="Firebase";
@Override
public void onTokenRefresh() {

    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);

}
}

AndroidManifest.xml -

<service
        android:name=".Helpers.MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>
    <service
        android:name=".Helpers.MyFirebaseInstanceIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>
 <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

Is it possible to get the Pending Notification when the app is relaunched ?

1条回答
时光不老,我们不散
2楼-- · 2020-08-03 04:39

i think it is possible since you're using a service, but android won't let you receive it until you add some sort of permission. Research for what permission will make this condition possible, and please update us.

add the following permission, then u should be good.
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <uses-permission android:name="android.permission.WAKE_LOCK" />

查看更多
登录 后发表回答