java – 当应用程序在后台时,无法从intent获取Extras
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 当应用程序在后台时,无法从intent获取Extras,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3461字,纯文字阅读大概需要5分钟。
内容图文
![java – 当应用程序在后台时,无法从intent获取Extras](/upload/InfoBanner/zyjiaocheng/797/3c70a3b366a64bbf984a83f4e37eabbb.jpg)
单击通知时,我尝试在MainActivity中执行一个函数.
该函数需要我放入intent extras的数据.
问题是当我在应用程序运行时单击通知执行该功能时,但当我在应用程序处于后台时单击通知时,该功能不会执行.我已经检查了它,因为当应用程序在后台时,我放入intent extras的数据是空的.
我怎么解决这个问题?谢谢!
这是我收到的回复:
{
"to":"blablabla",
"notification": {
"body":"Sentiment Negative from customer",
"title":"Mokita"
},
"data" : {
"room_id":1516333
}
}
这是我的通知代码:
public void onMessageReceived(RemoteMessage message) {
super.onMessageReceived(message);
Log.d("msg", "onMessageReceived: " + message.getData().get("room_id"));
String roomId = message.getData().get("room_id");
Intent intent = new Intent(this, HomePageTabActivity.class);
intent.putExtra("fromNotification", true);
intent.putExtra("roomId", roomId);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
String channelId = "Default";
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(message.getNotification().getTitle())
.setContentText(message.getNotification().getBody())
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
}
manager.notify(0, builder.build());
}
}
这是函数以及我如何在MainActivity中执行它:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer);
onNewIntent(getIntent());
}
@Override
public void onNewIntent(Intent intent){
Bundle extras = intent.getExtras();
if(extras != null){
if(extras.containsKey("fromNotification") || extras.containsKey("roomId")) {
openChatRoom(Long.valueOf(extras.getString("roomId")));
}else if(extras.containsKey("fromNotification") && extras.containsKey("roomId")){
openChatRoom(Long.valueOf(extras.getString("roomId")));
}else{
Log.e("EXTRAS room",""+extras.getString("roomId"));
Log.e("EXTRAS STATUS",""+extras.getBoolean("fromNotification"));
}
}else{
Toast.makeText(HomePageTabActivity.this,"Empty",Toast.LENGTH_SHORT).show();
}
}
public void openChatRoom(long roomId){
Log.d("LONG ROOM",""+roomId);
QiscusRxExecutor.execute(QiscusApi.getInstance().getChatRoom(roomId),
new QiscusRxExecutor.Listener<QiscusChatRoom>() {
@Override
public void onSuccess(QiscusChatRoom qiscusChatRoom) {
startActivity(GroupRoomActivity.
generateIntent(HomePageTabActivity.this, qiscusChatRoom));
}
@Override
public void one rror(Throwable throwable) {
throwable.printStackTrace();
}
});
}
解决方法:
Firebase有两种类型的消息:通知消息和数据消息.如果您希望FCM SDK自己处理消息,则需要使用通知.当应用程序处于非活动状态时,FCM将使用通知正文显示消息.在这种状态下,onMessageReceived也不会被触发.如果您希望应用处理消息,则需要使用数据.您可能需要更改推送通知有效负载
{
"message":{
"token":"xxxxx:...",
"notification":{
"title":"Your title",
"body":"Your message"
}
}
}
至
{
"message":{
"token":"xxxxx:...",
"data":{
"title":"Your title",
"body":"Your message",
"fromNotification":"true",
"roomId":"123"
}
}
}
您还需要相应地处理onMessageReceived(RemoteMessage remoteMessage)中的消息.您可以在Notifications and data messages中阅读有关通知行为的信息.
内容总结
以上是互联网集市为您收集整理的java – 当应用程序在后台时,无法从intent获取Extras全部内容,希望文章能够帮你解决java – 当应用程序在后台时,无法从intent获取Extras所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。