2016-07-28 4 views

답변

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

에서 구글의 예를 볼 수 있습니다 데이터 메시지는 항상 onMessageReceived 결과

  • 가 호출되는

  • 앱이 포 그라운드에있을 때 알림 메시지가 표시되면 onMessageReceived가 호출됩니다.

앱이 백그라운드에서 알림 메시지를 보내면 자동으로 생성 된 알림이 표시됩니다.

두 가지 유형의 FCM 메시지 here에 대해 자세히 알아보십시오.

click_action은 사용자가 자동 ​​생성 된 알림을 누를 때 실행되는 활동을 지정하는 데 사용할 수 있습니다. 지정되지 않은 경우 기본 활동이 시작됩니다. click_action은 현재 REST API를 통해서만 사용할 수 있습니다.

관련 문제