2017-09-02 6 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を作成する必要があり、あなたはサーバ鍵Firebaseコンソールプロジェクトで見つかったか、あなたがtoを持つ単一のユーザに送信するペイロードのexamepleとしてPOSTMAN GoogleのChrome拡張機能

を使用することができますPARAM:

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

また、to paramはあなたが望むものは何でも送ることができますdataで話題"to": "topics/yourTopic"

のために使用することができますが、メッセージはonMessageReceived()サービスFRで受信されます火の基盤

詳細は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"); 
関連する問題