Firebase云消息传递:无令牌和奇怪的通知行为

时间:2016-06-22 10:01:53

标签: android firebase-cloud-messaging

我遇到Firebase云消息传递问题: 我使用2个扩展服务FirebaseInstanceIdService和FirebaseMessagingService实现了一个小应用程序。

如果我启动应用程序并通过发送通知 Firebase控制台,我在设备上收到通知。 一切正常。 如果我再次打开应用程序,它会挂起并产生黑色 屏幕。那么Android Studio控制台上没有输出。 我也没有从Firebase控制台获得第二个通知。 然后我得到一个对话框:"应用程序没有反应等等#34; 再次打开应用程序后,它再次正常工作。

我也没有在Logcat(?)

中获得令牌

Firebase InstanceIdService:

public class InstanceIdService extends FirebaseInstanceIdService
{
    private static final String TAG = "InstanceIdService";

    @Override
    public void onTokenRefresh()
    {
        String token = FirebaseInstanceId.getInstance().getToken();
        Log.e(TAG, "!!!!!!!!!!!!!!!!!! Got token: " + token);
    }
}

FirebaseMessagingService:

 public class MyFirebaseMessagingService extends FirebaseMessagingService
{
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
    sendNotification(remoteMessage);
}

public void sendNotification(RemoteMessage remoteMessage)
{
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setContentTitle(remoteMessage.getFrom())
            .setContentText(remoteMessage.getNotification().getBody())
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notificationBuilder.build());
}
}

Android Studio控制台上唯一的错误是 "无法加载模块描述符类"但那就是 一个已知的错误。

我希望你能帮助我。

最诚挚的问候, 菲利克斯

编辑:错误消息来自Firebase控制台:

Exception java.lang.RuntimeException: Parcel android.os.Parcel@4215be98: Unmarshalling unknown type code 1936206469 at offset 68
android.os.Parcel.readValue (Parcel.java:2087)
android.os.Parcel.readArrayMapInternal (Parcel.java:2321)
android.os.Bundle.unparcel (Bundle.java:249)
android.os.Bundle.getString (Bundle.java:1118)
com.google.firebase.messaging.FirebaseMessagingService.zzT ()
com.google.firebase.messaging.FirebaseMessagingService.zzm ()
com.google.firebase.iid.zzb$2.run ()
java.util.concurrent.ThreadPoolExecutor.runWorker      (ThreadPoolExecutor.java:1112)
java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:587)
java.lang.Thread.run (Thread.java:841)

1 个答案:

答案 0 :(得分:3)

  

我也没有在Logcat(?)

中获得令牌
在为您生成新令牌时会调用

onTokenRefresh()方法。每次打开应用程序时都不会调用它。 但是,您可以使用FirebaseInstanceId.getInstance().getToken();

获取令牌
  

如果我启动应用程序并通过Firebase控制台发送通知,我会在设备上收到通知。一切正常。如果我再次打开应用程序,它会挂起并产生黑屏。

我遇到了类似的问题,如果您没有或网速较慢,通常会发生这种情况。还没有解决方案。 :(