2017-09-02 75 views
0

My MyFirebaseMessagingService。如何從我的服務中獲取數據或消息?

public class MyFirebaseMessagingService 
extends FirebaseMessagingService { 

private static final String TAG = "MyFirebaseMsgService"; 

    public void onMessageReceived(RemoteMessage remoteMessage) { 
     Log.d(TAG, "FRCM:"+ remoteMessage.getFrom()); 

     /*Check if the message contains data*/ 
     if(remoteMessage.getData().size() > 0){ 
      Log.d(TAG,"Message data: "+ remoteMessage.getData()); 

     } 
     /*Check if the message contains notification*/ 
     if(remoteMessage.getNotification() != null){ 
      Log.d(TAG,"Message body: "+ remoteMessage.getNotification().getBody()); 
      sendNotification(remoteMessage.getNotification().getBody()); 


     } 
    } 
    /*Display Notification Body*/ 
    private void sendNotification(String body) { 

     Intent intent = new Intent(this, Home.class); 
     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0/*Request code*/, intent, PendingIntent.FLAG_ONE_SHOT); 
     /*Set sound of Notification*/ 

     Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

     NotificationCompat.Builder notifiBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.ic_event_note_black_24dp) 
       .setContentTitle("Firebase Cloud Messaging") 
       .setContentText(body) 
       .setAutoCancel(true) 
       .setSound(notificationSound) 
       .setContentIntent(pendingIntent); 


     NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(0/*ID of notification*/, notifiBuilder.build()); 
     intent = new Intent("myAction"); 
     intent.putExtra("title", title); 
     intent.putExtra("message", message); 
     LocalBroadcastManager.getInstance(this).sendBroadcast(intent); 
    } 
} 

和我的活動消息是。

public class Mess{ 
} 
+0

讀? – phpdroid

+0

給我的消息活動命名爲Mess ..我應該如何從我的FirebaseMessagingService獲取數據...... –

回答

0

您使用FCM從手機發送消息。你需要做一個POSThttps://fcm.googleapis.com/fcm/send API與有效載荷要發送,並在火力地堡控制檯項目你服務器密鑰發現或者您可以使用郵差谷歌Chrome擴展

作爲有效載荷發送到單個用戶與to的exameple PARAM:

{ "data": { 
     "score": "5x1", 
     "time": "15:10" 
    }, 
    "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..." 
} 

此外,to PARAM可用於主題"to": "topics/yourTopic"

data您可以發送任何你想要的,在onMessageReceived()服務FR收到消息om Firebase。

更多細節可在Firebase documentation中找到。

+0

不,我的意思是如何獲取從firebasecloudmessaging接收的數據並傳遞給我的活動。 –

0

將數據發送到另一個活動

private void sendNotification(String body) { 

    Intent intent = new Intent(this, Home.class); 

在這裏你可以設置目的

intent.putExtra("word", body); 

和你在哪裏發送通知,從活動中使用

b = getIntent().getExtras(); 
String passed_from = b.getString("word"); 
相關問題