カスタム通知を扱うあなたのアプリがバックグラウンドで動作しているか、殺されたときFirebaseはあなたonMessageReceivedを呼び出すことはありません、あなたはあなたの通知をカスタマイズすることはできません。システムによって生成された通知が表示されます。あなたのすべての例でonMessageReived
a)はフォアグラウンド
b)の背景
c)を呼び出すためにfirebaseライブラリを作るために
はあなたがJSONキー "通知" を入れてはいけません
を殺しましたfirebase apiへのあなたの要求は、代わりに "データ"を使用してください、以下を参照してください。例えば
次のメッセージが、これは
{
"to": "/topics/test",
"data": {
"text":"text",
"title":"",
"line1":"testline",
"line2":"test"
}
}
は、このリンクを参照して動作しますonMessagereceived()
{
"to": "/topics/test",
"notification": {
"title" : "title",
"text": "data!",
"icon": "ic_notification"
}
}
けどを呼び出すことはありません。https://firebase.google.com/docs/cloud-messaging/concept-options それは例えばfirebaseメッセージタイプ の詳細な説明があります:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
if(remoteMessage.getData().get("state").toString().equals("Request")){
sendNotification(remoteMessage.getData().get("body").toString(), remoteMessage.getData().get("message").toString(), remoteMessage.getData().get("state").toString(),Activity_name.class);
}
}
private void sendNotification(String messageBody, String data, String state,Class<?> activityCompat) {
int requestID = (int) System.currentTimeMillis();
Intent intent = new Intent(this, activityCompat);
Bundle bundle = new Bundle();
bundle.putString("message", data);
bundle.putString("state", state);
intent.putExtras(bundle);
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.small_logo)
.setContentTitle("Title")
.setContentText(messageBody).setContentIntent(pendingIntent)
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(messageBody))
.setTicker(messageBody);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
Notification notification = notificationBuilder.build();
notificationManager.notify((int) Calendar.getInstance().getTimeInMillis(), notification);
}
1回通知はシステムトレイにプッシュされ、実際にはインテントエクストラにデータが保存され、通知はシステムトレイからタップされるとランチャーアクティビティから取得できます。 –
アプリが殺された場合やバックグラウンドであっても共有設定から通知ビューとカスタムサウンドを表示したい場合は、 – rafsanahmad007