我的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 :(得分:0)
您使用FCM从手机发送消息。您需要使用要发送的有效负载生成POST
到https://fcm.googleapis.com/fcm/send
api,并在Firebase控制台项目中找到服务器密钥,或者您可以使用POSTMAN google chrome扩展
作为使用to
param发送给单个用户的有效负载的exameple:
{ "data": {
"score": "5x1",
"time": "15:10"
},
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}
此外,to
param可用于主题"to": "topics/yourTopic"
在data
中,您可以发送任何内容,并在Firebase的onMessageReceived()
服务中收到消息。
您可以在Firebase documentation中找到更多详细信息。
答案 1 :(得分: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");