我添加了FCM服务,现在我必须以任何方式向所有设备发送通知
如果我使用设备令牌ID,我现在想使用Firebase消息传递服务从邮递员向所有设备发送通知,我会收到消息,但是如何发送给使用我的应用程序的所有用户
邮递员代码:
{
"to" : "/topics/refreshedToken",
"collapse_key" : "type_a",
"notification" : {
"body" : "Test Notification",
"click_action": "PRO",
"title": "Hello ?"
},
"data" : {
"body" : "GET CREDITS NOW",
"click_action":"PRO",
"title": "data title",
"key_1" : "Value1",
"key_2" : "Value2"
}
}
Firebase ID服务:
public class FirebaseIDService extends FirebaseInstanceIdService {
private static final String TAG = "FirebaseIDService";
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.e(TAG, "Refreshed token: " + refreshedToken);
// TODO: Implement this method to send any registration to your app's servers.
sendRegistrationToServer(refreshedToken);
FirebaseMessaging.getInstance().subscribeToTopic(refreshedToken);
FirebaseMessaging.getInstance().subscribeToTopic("com.thetechroot.vision");
}
/**
* Persist token to third-party servers.
*
* Modify this method to associate the user's FCM InstanceID token with any server-side account
* maintained by your application.
*
* @param token The new token.
*/
private void sendRegistrationToServer(String token) {
// Add custom implementation, as needed.
}
}
FirebaseMessaging服务类:
public class MyFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
private static final String TAG = "FirebaseMessagingServic";
public MyFirebaseMessagingService() {
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//To Get FCM TOKEN
//eTBuGS-wrO4:APA91bGb1zBE1u9MIS1QrHo7MFaLfUROlAoDjZOpt7sdkLUpWe7K-yFcbYWxg1cMQPsl4vFAtuIr_N4MLkVlNQ93x0aRpAwP6Xw30M6PBWhC-R-GWLIJUgkOsDd4jAcwcdMUBeUTiS3g
Log.d("Firebase", "token "+ FirebaseInstanceId.getInstance().getToken());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
try {
JSONObject data = new JSONObject(remoteMessage.getData());
String jsonMessage = data.getString("extra_information");
Log.d(TAG, "onMessageReceived: \n" +
"Extra Information: " + jsonMessage);
} catch (JSONException e) {
e.printStackTrace();
}
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
String title = remoteMessage.getNotification().getTitle(); //get title
String message = remoteMessage.getNotification().getBody(); //get message
String click_action = remoteMessage.getNotification().getClickAction(); //get click_action
Log.d(TAG, "Message Notification Title: " + title);
Log.d(TAG, "Message Notification Body: " + message);
Log.e(TAG, "Message Notification click_action: " + click_action);
sendNotification(title, message,click_action);
}
}
@Override
public void onDeletedMessages() {
}
private void sendNotification(String title,String messageBody, String click_action) {
Intent intent;
Log.e(TAG,"@@@ Click action ::> " + click_action);
if(click_action.equals("DEMOACTIVITY")){
intent = new Intent(this, DemoActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
else if(click_action.equals("MAINACTIVITY")){
intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
else if(click_action.equals("PRO")){
intent = new Intent(this, PriceListActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}else{
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.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
现在如何向所有设备发送通知并更改邮递员代码中的“至”。
答案 0 :(得分:1)
您可以订阅主题"all"
FirebaseMessaging.getInstance().subscribeToTopic("all");
在Postman
{
"to" : "/topics/all",
// Rest of the JSON
}
您还订阅了refreshedToken
,这实际上不是必需的,因为默认情况下每个设备都订阅了其token
。
您还订阅了com.thetechroot.vision
FirebaseMessaging.getInstance().subscribeToTopic("com.thetechroot.vision");
因此,在Postman
中,您也可以这样做
{
"to" : "/topics/com.thetechroot.vision",
// Rest of the JSON
}