Rif*_*ana 5 java android android-intent android-notifications android-pendingintent
单击通知时,我尝试在 MainActivity 中执行一个函数。该函数需要一个我放入意图附加项的数据。
问题是当我在应用程序运行时单击通知时执行函数,但是当我在应用程序在后台时单击通知时,函数没有执行。我已经检查过了,这是因为当应用程序在后台时,我放入 Intent Extras 的数据是空的。
我怎么解决这个问题?谢谢!
这是我收到的回复:
{
"to":"blablabla",
"notification": {
"body":"Sentiment Negative from customer",
"title":"Mokita"
},
"data" : {
"room_id":1516333
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的通知代码:
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());
}
Run Code Online (Sandbox Code Playgroud)
}
这是函数以及我如何在 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 onError(Throwable throwable) {
throwable.printStackTrace();
}
});
}
Run Code Online (Sandbox Code Playgroud)
Firebase 有两种类型的消息:通知消息和数据消息。如果您希望 FCM SDK 自行处理消息,则需要使用notification. 当应用程序处于非活动状态时,FCM 将使用notification正文来显示消息。在这种状态下,onMessageReceived也不会被触发。如果您希望应用程序处理消息,则需要使用data. 您可能需要将推送通知有效负载从
{
"message":{
"token":"xxxxx:...",
"notification":{
"title":"Your title",
"body":"Your message"
}
}
}
Run Code Online (Sandbox Code Playgroud)
到
{
"message":{
"token":"xxxxx:...",
"data":{
"title":"Your title",
"body":"Your message",
"fromNotification":"true",
"roomId":"123"
}
}
}
Run Code Online (Sandbox Code Playgroud)
您还需要相应地处理消息onMessageReceived(RemoteMessage remoteMessage)。您可以在通知和数据消息中了解通知行为。