2016-07-28 8 views
1

通知をクリックするとアプリケーションがフォアグラウンドまたはバックグラウンドになると、アプリケーションはonMessageReceivedイベントを呼び出します。私は通知のclick_actionを使用します。それは正しい?通知をクリックするとfirebase onMessageReceivedが呼び出されます

アプリがフォアグラウンドにあるときに通知を作成し、通知をクリックすると、メソッドを再度実行して別の通知を作成します。

答えて

2

onMessageReceivedは、アンドロイドクライアントがFirebase Cloudからメッセージを受信したときに呼び出されるメソッドです。通常、このメソッドで通知を作成する関数を作成します。

通知をクリックすると、pendingIntentを使用できます。

私たちは呼ばれonMessageReceived物事のカップルに依存するかどうかthis github repo

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

private static final String TAG = "MyFirebaseMsgService"; 

// [START receive_message] 
@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 


    // TODO(developer): Handle FCM messages here. 
    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 
    sendNotification(remoteMessage.getNotification().getBody()); 
} 
// [END receive_message] 

/** 
* Create and show a simple notification containing the received FCM message. 
* 
* @param messageBody FCM message body received. 
*/ 
private void sendNotification(String messageBody) { 
    Intent intent = new Intent(this, MainActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
      PendingIntent.FLAG_ONE_SHOT); 

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.ic_stat_ic_notification) 
      .setContentTitle("FCM Message") 
      .setContentText(messageBody) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

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

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
} 
} 
2

にGoogleからの例を見ることができます:データメッセージが常にonMessageReceivedになり

  • が呼び出され

  • アプリケーションがフォアグラウンドにあるときに通知メッセージが表示され、onMessageReceivedが呼び出されます。

アプリがバックグラウンドにあり、通知メッセージを送信すると、自動的に生成された通知が表示されます。

2種類のFCMメッセージhereの詳細を参照してください。

click_actionを使用すると、ユーザーが自動生成された通知をタップしたときに起動されるアクティビティを指定できます。指定されていない場合、デフォルトのアクティビティが起動されます。 click_actionは、現時点ではREST APIを介してのみ使用できます。

関連する問題