0

我迁移到 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"/>

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

4

1 回答 1

3

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

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

于 2016-06-23T17:29:21.657 回答