当我的应用程序打开并收到通知时,我希望能够立即打开关联的活动,而无需用户点击通知.
When my application is open and I receive a notification I want to be able to open the activity associated immediately without the need of the user to tap on the notification.
这个问题非常相似:收到 Firebase 通知时打开应用程序 (FCM)
但它在后台打开应用程序,我需要在我的应用程序在前台时这样做.
But it opens the app when it is in background, I need to do it when my app is in foreground.
来自 firebase 文档:
当您的应用在后台时发送的通知.在这个在这种情况下,通知会发送到设备的系统托盘.一个默认情况下,用户点击通知会打开应用启动器.留言具有通知和数据有效负载,背景和前景.在这种情况下,通知将发送到设备的系统托盘,数据负载在附加组件中交付启动器 Activity 的意图.
Notifications delivered when your app is in the background. In this case, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default. Messages with both notification and data payload, both background and foreground. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
这是我对 onMessageReceived
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
sendNotification( remoteMessage);
}
}
/**
* Create and show a simple notification containing the received FCM message.
*
* @param remoteMessage FCM message message received.
*/
private void sendNotification(RemoteMessage remoteMessage) {
Intent intent = new Intent(this, MyActivity.class);
Map<String, String> hmap ;
hmap = remoteMessage.getData();
hmap.get("data_info");
intent.putExtra("data_info", hmap.get("data_info"));
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
}
我能够正确收到通知,但只有在我点击系统托盘上的通知后才会开始活动.
I am able to get the notification correctly but the activity only starts once I tap the notification on the system tray.
有没有办法在前台不点击通知来启动活动?
Is there a way to start the activity without tapping the notification while in foreground?
MyFirebaseMessagingService
类中的 onMessageReceived()
方法 extends FirebaseMessagingService
在前台被正确调用,但活动没有得到开始了.我也尝试过使用标志 FLAG_ACTIVITY_NEW_TASK
也没有运气.提前致谢.
The method onMessageReceived()
from the class MyFirebaseMessagingService
that extends FirebaseMessagingService
is getting called correctly while in foreground, but the activity is not getting started. I have also tried with the flag FLAG_ACTIVITY_NEW_TASK
also with no luck. Thanks in advance.
您可以在前台活动中注册广播接收器并从您的 onReceiveMessage() 方法发送广播.
You can achieve that registering a broadcast receiver in you foreground activity and sending a broadcast from your onReceiveMessage() method.
前台活动
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Intent myNewActivity = new Intent(this, MyActivity.class);
startActivity(myNewActivity);
}
};
mIntentFilter=new IntentFilter("OPEN_NEW_ACTIVITY");
@Override
protected void onResume() {
super.onResume();
registerReceiver(mReceiver, mIntentFilter);
}
@Override
protected void onPause() {
if(mReceiver != null)
unregisterReceiver(mReceiver);
mReceiver = null;
}
super.onPause();
}
FirebaseNotificationReceiver
FirebaseNotificationReceiver
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
sendNotification( remoteMessage);
Intent broadcast = new Intent();
broadcast.setAction("OPEN_NEW_ACTIVITY);
sendBroadcast(broadcast);
}
}
您可以添加检查以了解 应用是否在前台 或不选择发送通知或发送广播.
You can add a check to know if the app is in foreground or not to choose between send a notification or send a broadcast.
这篇关于在前台收到的关于 Firebase 通知的开放活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!