FCM - 重新启动应用程序时未接收通知

时间:2016-06-23 17:15:44

标签: android notifications firebase firebase-notifications

我迁移到Firebase Cloud Messaging以获取推送通知。每件事情都很好,我会在应用程序打开或在后台运行时收到通知,但是当应用程序关闭并且从服务器发送消息时,您就不会在设备中收到消息(显然)。现在,当我重新打开应用程序时,我应该收到待处理通知(应用程序关闭时发送的通知)但我没有收到通知。

这是我的FireBaseMessagingService类

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());
}
}

这是我的FirebaseInstance类

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"/>

重新启动应用时是否可以获得待处理通知?

1 个答案:

答案 0 :(得分:3)

我认为这是可能的,因为你正在使用服务,但是在你添加某种权限之前,android不会让你收到它。研究许可将使这种情况成为可能,并请更新我们。

添加以下权限,那么你应该是好的。
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <uses-permission android:name="android.permission.WAKE_LOCK" />