我目前正在为我的应用程序编写一项功能,允许用户向工作人员提出请求,他们可以接受或拒绝工作机会。我正在使用火力消息传递系统。当我有两部手机时,通知有效,工作人员可以接受或拒绝,如下所示:
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 回调。如果应用程序在后台运行,则会显示通知,但它不会触发 onMessageReceivedeved,并且该消息中的数据将传递到启动的意图。 您可以使用:-
getIntent().getExtras();
以检索通过通知发送的数据。
查看文档