gpt4 book ai didi

javascript - 如何向所有设备发送 FCM 通知?如何获取不同设备的token

转载 作者:行者123 更新时间:2023-12-03 00:49:04 25 4
gpt4 key购买 nike

我添加了 FCM 服务,现在我必须以任何方式向所有设备发送通知

我现在想使用 firebase 消息服务从 postman 向所有设备发送通知,如果我使用我的设备 token ID,我会收到消息,但如何发送给使用我的应用程序的所有用户

postman 代码:

{
"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());
}
}

现在如何向所有设备发送通知并更改 postman 代码中的“to”。

最佳答案

您可以订阅主题“全部”

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
}

关于javascript - 如何向所有设备发送 FCM 通知?如何获取不同设备的token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53132127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com