java - Firebase 消息传递仅适用于单独的设备
问题描述
我目前正在为我的应用程序编写一个功能,该功能允许用户向工人提出请求,他们可以接受或拒绝工作机会。我正在使用firebase消息传递系统。当我有两部手机时,通知有效,工作人员可以接受或拒绝,如下所述:
public class MyFirebaseMessaging extends FirebaseMessagingService {
private static final String COLE_CHANNEL_ID = "com.example.usub.COLE";
@Override
public void onMessageReceived(RemoteMessage remoteMessage){
LatLng customer_location = new Gson().fromJson(remoteMessage.getNotification().getBody(),LatLng.class);
Intent intent = new Intent(getBaseContext(), CustomerCall.class);
intent.putExtra("lat", customer_location.latitude);
intent.putExtra("lng", customer_location.longitude);
intent.putExtra("customer", remoteMessage.getNotification().getTitle());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
但是,我尝试在我的手机上运行涉及这两个应用程序的演示,我收到已发出请求的通知,但是当单击通知时,应用程序打开时不记得数据。我的通知助手如下所示:
public class NotificationHelper extends ContextWrapper {
private static final String COLE_CHANNEL_ID = "com.example.usub.COLE";
private static final String COLE_CHANNEL_NAME = "STRADTMANNSOLUTIONS Usub";
private NotificationManager manager;
public NotificationHelper(Context base) {
super(base);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
createChannels();
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void createChannels() {
NotificationChannel coleChannels = new NotificationChannel(COLE_CHANNEL_ID,
COLE_CHANNEL_NAME,
NotificationManager.IMPORTANCE_DEFAULT);
coleChannels.enableLights(true);
coleChannels.enableVibration(true);
coleChannels.setLightColor(Color.GRAY);
coleChannels.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
getManager().createNotificationChannel(coleChannels);
}
public NotificationManager getManager() {
if(manager == null)
manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
return manager;
}
@RequiresApi(api = Build.VERSION_CODES.O)
public Notification.Builder getUsubNotification(String title, String content, PendingIntent contentIntent,
Uri soundUri)
{
return new Notification.Builder(getApplicationContext(),COLE_CHANNEL_ID)
.setContentText(content)
.setContentTitle(title)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_notify);
}
}
有谁知道为什么通知会丢失其所有数据,或者我如何使用通知消息中的新信息打开应用程序以进行特定活动?谢谢
解决方案
仅当您的应用程序处于前台时,通知消息才会传递到您的 onMessageReceived 回调。如果应用程序在后台,则会显示一个通知,但不会触发 onMessageRecieved,并且该消息中的数据将传递给启动的意图。你可以使用: -
getIntent().getExtras();
检索通过通知发送的数据。
检查这个文档
推荐阅读
- ios - 有没有办法在没有内部缓存的情况下从 CGImage 读取数据?
- javascript - Get a Tag Attribute Value when Tab is active
- ruby-on-rails - 我做对了吗(模型内部的方法)以及如何删除重复项
- vue.js - 如何像用户在浏览器中按 F5 一样重置 vue.js 应用程序?
- report - Acumatica:如何使用报表设计器在一个报表中同时显示行和行详细信息?
- python - 通过特定字符串重塑 Pandas 数据框
- javascript - SAPUI5 Javascript - 获取每个唯一属性的数组的第一个和最后一个元素
- angular - 单击 Angular 6 时更改模板
- jquery - 在上下移动时保持元素始终可见
- c# - C#读取excel单元格获得不同的值,在屏幕上看到