2017-02-28 38 views
1

我使用fcm實施了擡頭通知。 當應用程序收到fcm通知時,如果我的應用程序正在運行,擡頭通知會顯示在屏幕上。那很好。爲什麼擡頭通知在應用未運行時不顯示

但是,如果我的應用程序是背景或死亡,擡頭通知不會顯示。 我該如何解決這個問題? (也許我想,如果當收到通知FCM我的應用程序正在運行,MyFirebaseMessagingService工作良好。但是,如果我的應用程序是背景或被殺,MyFirebaseMessagingService類不工作)

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     Map<String, String> data = remoteMessage.getData(); 
     sendNotification(remoteMessage); 
    } 

    private void sendNotification(RemoteMessage message) { 

     Intent push = new Intent(this, SplashActivity.class); 
     push.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
       | Intent.FLAG_ACTIVITY_CLEAR_TASK 
       | Intent.FLAG_ACTIVITY_NEW_TASK); 
     PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 0, push, PendingIntent.FLAG_CANCEL_CURRENT); 
     NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.ic_launcher) 
       .setWhen(System.currentTimeMillis()) 
       .setContentTitle("test") 
       .setContentText(message.getNotification().getBody()) 
       .setCategory(NotificationCompat.CATEGORY_MESSAGE) 
       .setVibrate(new long[] {0}) 
       .setDefaults(Notification.DEFAULT_ALL) 
       .setAutoCancel(true) 
       .setPriority(NotificationCompat.PRIORITY_HIGH) 
       .setContentIntent(fullScreenPendingIntent); 
     NotificationManager nm = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     nm.notify(1, builder.build()); 

    } 


} 

回答

1

有兩種類型的FCM消息被可用。

  1. 通知消息,有時被認爲是「顯示消息」。

  2. 數據消息,由客戶端應用程序處理。

通知消息不會顯示。當你的應用程序沒有運行或死亡。

請檢查以下鏈接,它會幫助你。

https://firebase.google.com/docs/cloud-messaging/concept-options

+0

你是說我的問題不是我的錯? – Dennis

+0

是的,請查看鏈接。你可以得到一些想法。 – Thirumalvalavan

+0

謝謝。我認爲你是一個天使 – Dennis

相關問題