Android 如何使用Json文件初始化RemoteMessage对象?
我正在处理FCM消息,我具有以下功能:Android 如何使用Json文件初始化RemoteMessage对象?,android,json,firebase,firebase-cloud-messaging,Android,Json,Firebase,Firebase Cloud Messaging,我正在处理FCM消息,我具有以下功能: public void onMessageReceived(final RemoteMessage remoteMessage) { final Map<String, String> data = remoteMessage.getData(); //... } 您可以为单个用户使用Postman进行测试 使用JSON负载发送通知 网址: 标题: Authorization: key=<your-api-key
public void onMessageReceived(final RemoteMessage remoteMessage)
{
final Map<String, String> data = remoteMessage.getData();
//...
}
您可以为单个用户使用Postman进行测试 使用JSON负载发送通知 网址: 标题:
Authorization: key=<your-api-key>
Content-Type: application/json
来源:您可以为单个用户使用Postman进行测试 使用JSON负载发送通知 网址: 标题:
Authorization: key=<your-api-key>
Content-Type: application/json
资料来源:TL;DR:无法初始化对象
尝试从您自己的类访问或初始化RemoteMessage将返回错误: “RemoteMessage(android.os.Bundle)”在“com.google.firebase.messaging.RemoteMessage”中不是公共的。无法从包外部访问 我猜想您是为了测试目的而尝试这样做的(存根?)。一般来说(不是说最佳实践,因为我不是很确定它是最佳实践),建议使用一个单独的方法来接受您需要的特定值。参考(删除了一些内容): 关注提到以下内容的评论: 此外,如果您打算根据收到的FCM消息生成您自己的通知,则应在此处启动。请参阅下面的sendNotification方法 这指出了接受字符串参数的
sendNotification()
方法。根据您的情况,您可以简单地传递方法所需的对象,而不是尝试传递初始化的RemoteMessage
(很遗憾,这是不可能的,或者至少不可取的)
有了它,您几乎可以测试处理消息的方法,而不依赖于一个不是您最初创建的类的对象
附言:我的一些解释可能令人困惑,但我希望这是有意义的 TL;DR:无法初始化对象
尝试从您自己的类访问或初始化RemoteMessage将返回错误: “RemoteMessage(android.os.Bundle)”在“com.google.firebase.messaging.RemoteMessage”中不是公共的。无法从包外部访问 我猜想您是为了测试目的而尝试这样做的(存根?)。一般来说(不是说最佳实践,因为我不是很确定它是最佳实践),建议使用一个单独的方法来接受您需要的特定值。参考(删除了一些内容): 关注提到以下内容的评论: 此外,如果您打算根据收到的FCM消息生成您自己的通知,则应在此处启动。请参阅下面的sendNotification方法 这指出了接受字符串参数的
sendNotification()
方法。根据您的情况,您可以简单地传递方法所需的对象,而不是尝试传递初始化的RemoteMessage
(很遗憾,这是不可能的,或者至少不可取的)
有了它,您几乎可以测试处理消息的方法,而不依赖于一个不是您最初创建的类的对象
附言:我的一些解释可能令人困惑,但我希望这是有意义的 以JSONObject json=new JSONObject(“您的json字符串”)的形式传递数据;在onMessageReceived方法内部。@Janak但我如何将其转换为remoteMessage对象?您想在响应中使用此Json吗?@Janak我想将此Json传递给
onMessageReceived()
,因此我必须将其转换为remoteMessage
对象。我的问题是如何做到这一点;在onMessageReceived方法内部。@Janak但我如何将其转换为remoteMessage对象?您想在响应中使用此Json吗?@Janak我想将此Json传递给onMessageReceived()
,因此我必须将其转换为remoteMessage
对象。我的问题是如何做到这一点。
{
"to": "dQYmpLUACXQ:APA91bGl-NoIMJ2_DcctF5-OA8ghyWuyrMfsz3uhlj1BySl6axkAsmv5y_7YGfpQQJ2E0lP_fTcxpHpZdkJzY1tbcWA36e78ooxC_b0a1PAank9gFIAUHVZkHKmZT70MPZosCgvRlVfq",
"data": {
"id": "4422",
"type": "1",
"imageUrl": "https://image.freepik.com/free-vector/android-boot-logo_634639.jpg",
"smallTitle": "DoJMA v2",
"smallSubTitle": "Update now from Google Play Store",
"ticker": "New update for DoJMA",
"contentInfo": "",
"link": "https://photo2.tinhte.vn/data/avatars/l/1885/1885712.jpg?1402763583",
"className": "HomeActivity",
"page": "2",
"bigTitle": "DoJMA Android app version 2 released!",
"bigSubTitle": "Hi folks! New DoJMA update is here! Major redesigning and improvements! This app was made by the Mobile App Club.They work really hard man...and get good products",
"bigSummaryText": "Update now"
}
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
if (/* Check if data needs to be processed by long running job */ true) {
// For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
scheduleJob();
} else {
// Handle message within 10 seconds
handleNow();
}
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}
/**
* Schedule a job using FirebaseJobDispatcher.
*/
private void scheduleJob() {
// [START dispatch_job]
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
Job myJob = dispatcher.newJobBuilder()
.setService(MyJobService.class)
.setTag("my-job-tag")
.build();
dispatcher.schedule(myJob);
// [END dispatch_job]
}
/**
* Handle time allotted to BroadcastReceivers.
*/
private void handleNow() {
Log.d(TAG, "Short lived task is done.");
}
/**
* Create and show a simple notification containing the received FCM message.
*
* @param messageBody FCM message body received.
*/
private void sendNotification(String messageBody) {
Intent 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.drawable.ic_stat_ic_notification)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}