2017-03-16 23 views
5

フォアグラウンドだけでなくバックグラウンドでもfirebase通知メッセージを処理したい。私は開発者からのyoutubeリンクを構成するメッセージを送信します。ユーザーが通知バーをタップすると、リンクを開くようユーザーに指示する必要があります。誰がそれがどのように行われているか知っていますか?バックグラウンドとフォアグラウンドでのfirebase通知の処理方法は?

public void onMessageReceived(RemoteMessage remoteMessage) { 
    // [START_EXCLUDE] 
    // There are two types of messages data messages and notification messages. Data messages are handled 
    // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type 
    // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app 
    // is in the foreground. When the app is in the background an automatically generated notification is displayed. 
    // When the user taps on the notification they are returned to the app. Messages containing both notification 
    // and data payloads are treated as notification messages. The Firebase console always sends notification 

    // [END_EXCLUDE] 

    // TODO(developer): Handle FCM messages here. 
    // Not getting messages here? See why this may be: 
    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()); 
    } 

    // Check if message contains a notification payload. 
    if (remoteMessage.getNotification() != null) { 
     Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); 
    } 

    // Also if you intend on generating your own notifications as a result of a received FCM 
    // message, here is where that should be initiated. See sendNotification method below. 
} 

私の目標を達成するために次に何をしますか?ありがとうございます。

+0

掲載されたコードは、サンプルに含まれているものです。だから私はあなたがそれらを通過したと仮定しています。それについて何か混乱していましたか?あなたはまた、ドキュメントを行ったことがありますか?今、これは私のコード*の質問と同じです。 –

+0

私のソリューションを試してみませんか?
私はすでに試してみました。
それは動作します
https://stackoverflow.com/questions/48897883/how-to-handle-notifications-with-fcm-when-app-is-in-either-foreground-or-backgro/48899186#48899186 –

答えて

4

FCMメッセージにデータペイロードを送信する必要があります。データペイロードは、アプリケーションがフォアグラウンドまたはバックグラウンドであるかどうかにかかわらず、メッセージメソッドで受信されます。そこの行動を扱います。データペイロードを常に読み込んで、またはアプリが開いているときやフォアグラウンドで警告ダイアログを表示したい場合は、通知を表示します。ここ

はサンプルペイロードである:次に

{ 
    "to": "registration_id_or_topic", 
    "data": { 
     "message": "This is a Firebase Cloud Messaging Topic Message!", 
     "youtubeURL": "https://youtu.be/A1SDBIViRtE" 
    } 
} 

あなたonMessageReceived中:

public void onMessageReceived(RemoteMessage remoteMessage) { 
    if (remoteMessage.getData().size() > 0) { 
     Log.d(TAG, "Message data payload: " + remoteMessage.getData()); 
     Map<String, String> receivedMap = remoteMessage.getData(); 
     String youtubeURL = receivedMap.get("youtubeURL"); 
     showNotificationWithURLAction(youtubeURL); 
    } 
    ..... 
} 

あなたは簡単にそれをグーグルでshowNotificationWithURLAction(...)メソッドを実装することができます。

Intent notificationIntent = new Intent(Intent.ACTION_VIEW); 
     notificationIntent.setData(Uri.parse(remoteMessage.getData().getString("url")); 
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 
     // Resources r = getResources(); 
      Notification notification = new NotificationCompat.Builder(context) 
        .setTicker("yortext") 
        .setSmallIcon(android.R.drawable.ic_menu_report_image) 
        .setContentTitle("title") 
        .setContentText("content") 
        .setContentIntent(pendingIntent) 
        .build(); 

      NotificationManager notificationManager = (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE); 
      notificationManager.notify(0, notification); 

がfirebaseコンソールに入るペイロードを送信するには:あなたはディープリンクを使用するよりもあなたのアプリ内のコンテンツにユーザーをリダイレクトしたい場合は、1個のサンプルがhere

+0

ペイロードにリンクを送信する方法と、ユーザーがクリックしたときにそのリンクを処理する方法助けてください –

+0

@ Lindani Masingaによって下記の答えを見てください。私も私の答えを更新します – drulabs

+0

コンソールからデータペイロードを送信するには@ Drake29aによって答えを参照してください – drulabs

0

あり、ここで はどのようにちょうどオープンの通知がクリックされたリンクです新しいメッセージを作成します。ここには、データをキー/値のペアに入れることができる高度なオプションがあります。URLを下の写真のように入力します。

Adding payload to Firebase message

+0

私の質問を投票しないでください。 –

+0

これはOPで質問された問題を解決しません。 – drulabs

+0

ドレイクはfbで私の友人のリクエストを受け入れます。ファイアベースに関してもう少し疑問を抱きたいです。@ Drake29a –

1

これはthis questionの可能重複しています。プッシュメッセージ本文に「通知」を送信しないでください。

だから、基本的に、私はからのプッシュ通知要求のために身体を変更:へ

{ 
    "data": { 
     "type" : "mytype" 
    }, 
    "notification": { 
     "title": "My Title", 
     "body": "My Notification Message" 
    }, 
    "to": "/topics/all" 
} 

{ 
    "data": { 
     "type" : "mytype", 
     "title": "My Title", 
     "body": "My Notification Message" 
    }, 
    "to": "/topics/all" 
} 

今、私のアプリもバックグラウンドでonMessageReceived()毎回を呼び出し、私はちょうど変更得られた通知のタイトルとメッセージをプッシュデータに使用する方法。

+0

こんにちは、私のアプリはバックグラウンドでonMessageReceived()を呼び出さない。 – Avi

0

アンドロイドシステムは、ペイロードの通知フィールド内のメッセージを常に処理します。あなたのアプリが通知を処理するようにするには、あなたのペイロードのデータフィールドの中にYouTubeのリンクを入れる必要があります。

{ "data" : 
    { "link" : "www.youtube.com"} 
} 

この通知は、アプリケーションでRemoteMessageとして受信されます。 remoteMessage.getData()を使用してyoutubeのリンクを取得します。

https://firebase.google.com/docs/cloud-messaging/concept-options#messages-with-both-notification-and-data-payloads

+0

こんにちは、私のアプリはバックグラウンドでonMessageReceived()を呼び出さない。 – Avi

関連する問題