2

使用FCM,當應用程序處於後臺或未運行時,系統托盤中會收到推送通知。當應用程序在前臺時,我可以覆蓋onMessageReceived並使用NotificationCompat創建我自己的單挑通知。Firebase消息傳遞 - 在後臺應用程序中創建擡頭顯示

有沒有辦法在我的應用程序處於後臺或未運行時創建擡頭通知?

感謝

編輯:僅供參考這裏的消息有效載荷https://fcm.googleapis.com/fcm/send

{ 
    "to":"push-token", 
    "content_available": true, 
    "priority": "high", 
    "notification": { 
     "title": "Test", 
     "body": "Mary sent you a message!", 
     "sound": "default" 
    }, 
    "data": { 
     "message": "Mary sent you a Message!", 
     "notificationKey":"userID/notification_type", 
     "priority": "high", 
     "sound": "default" 
    } 
} 

回答

1

我通過捲曲僅使用,如果你使用的是一些其他的應用程序時,您會得到擡起頭來通知你應用程序在後臺或未運行。如果您的手機未被使用,您將收到系統托盤通知或鎖定屏幕通知。

如果您正在使用應用服務器通過http協議發送推送通知,那麼您甚至可以在發送到fcm端點的json數據中將優先級設置爲高。

如果您使用Firebase控制檯,則在高級通知部分設置下確保優先級高。

高優先級將確保您在大多數情況下收到擡頭通知。

編輯:這是你編輯的JSON看起來應該像成功的測試 -

{ 
    "to":"push-token", 
    "priority": "high", 
    "notification": { 
     "title": "Test", 
     "body": "Mary sent you a message!", 
     "sound": "default", 
     "icon": "youriconname" 
    } 
} 

youriconname是要設置爲您的通知圖標繪製資源的名稱。

我爲測試目的省略了數據。正是這麼多,應該讓你領導通知。

+1

感謝您的回覆。我通過curl將我使用的JSON有效載荷添加到了我的問題中。我只是加倍檢查,當我在另一個應用程序(或應用程序未運行)時收到聲音通知,但我根本沒有收到單獨通知。 –

+0

看着你的json。我會立即建議你兩件事 - 從數據**中刪除**聲音和優先級。只保存在一個位置。另外**除非您爲iOS開發**,否則不需要設置content_available。一旦使用此設置進行測試,並在完全移除數據的情況下進行測試,則只會按照建議保留通知,然後查看其行爲。 –

+0

另請注意,您不必在使用fcm時使用通知兼容構建器進行通知。由於fcm會自己處理它。我現在還要求您刪除任何此類通知代碼。一旦你從純粹的fcm得到正確的頭像通知,那麼你可以進一步自定義。** –

3

我找到了解決辦法: 我只是從我們的本地服務器,然後我生成MyFirebaseMessagingService回調發送到服務器火力JSON刪除通知標記:onMessageReceived()方法。 n此方法我使用NotificationCompat.Builder類生成本地通知。這裏是Android的代碼:

private void sendNotification(RemoteMessage remoteMessage) { 
     Intent intent = new Intent(this, SplashActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     intent.putExtra(AppConstant.PUSH_CATEGORY, remoteMessage.getData().get("category")); 
     intent.putExtra(AppConstant.PUSH_METADATA, remoteMessage.getData().get("metaData")); 
     intent.putExtra(AppConstant.PUSH_ACTIVITY, remoteMessage.getData().get("activity")); 
     intent.putExtra(AppConstant.PUSH_ID_KEY, remoteMessage.getData().get("_id")); 

     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) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentTitle(remoteMessage.getData().get("title")) 
       .setContentText(remoteMessage.getData().get("body")) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setPriority(NotificationCompat.PRIORITY_HIGH) 
       .setContentIntent(pendingIntent); 

     NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     notificationManager.notify(0, notificationBuilder.build()); 
    } 
相關問題