如何判断 Android 通知栏中的应用程序是否已打开?例如,我已经关闭了该应用程序(从最近使用的应用程序列表中清除)。但是,我从后端 WebSocket 收到通知,我按下它,它会打开该应用程序。所以我的问题是,有没有办法检查是否是从通知打开的应用程序?
如何判断 Android 通知栏中的应用程序是否已打开?例如,我已经关闭了该应用程序(从最近使用的应用程序列表中清除)。但是,我从后端 WebSocket 收到通知,我按下它,它会打开该应用程序。所以我的问题是,有没有办法检查是否是从通知打开的应用程序?
查看react-native-push-notification源代码及接下来的50行(直到setContentIntent
),您可以检查意图中的“通知”额外信息。
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getBundleExtra("notification");
if(bundle != null){
//check if it is the bundle of your notification and do your thing
}
}
.setContentIntent()
方法的PendingIntent
时,指定一个在应用程序中恢复的操作。示例通知:Intent intent = new Intent(context, MyActivity.class);
intent.setAction("OPEN_MY_APP_FROM_NOTIFICATION");
NotificationCompat.Builder mNotifyBuilder = NotificationCompat.Builder(this, CHANNEL)
.setContentTitle("Title")
.setContentIntent(PendingIntent.getActivity(this, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT))
mNotificationManager.notify(Notification_REQUEST_CODE,
mNotifyBuilder.build())
在MyActivity.java中
public void onCreate (Bundle savedInstanceState) {
// Get intent, action and MIME type
Intent intent = getIntent();
String action = intent.getAction();
if(action == "OPEN_MY_APP_FROM_NOTIFICATION"){
//do whatever you have to do here
}
}
import PushNotification from 'react-native-push-notification'
configurePushNotifications = () => {
PushNotification.configure({
// (optional) Called when Token is generated (iOS and Android)
onRegister: function(token) {
console.log('PushNotification token', token)
},
onNotification是您接收本地或远程通知的位置,当用户点击通知栏时,它将被调用。
onNotification: function(notification) {
console.log('notification received', notification)
},
// IOS ONLY (optional): default: all - Permissions to register.
permissions: {
alert: true,
badge: true,
sound: true,
},
// Should the initial notification be popped automatically
// default: true
popInitialNotification: true,
/**
* (optional) default: true
* - Specified if permissions (ios) and token (android and ios) will requested or not,
* - if not, you must call PushNotificationsHandler.requestPermissions() later
*/
requestPermissions: true,
})
}
{
foreground: false, // BOOLEAN: If the notification was received in foreground or not
userInteraction: false, // BOOLEAN: If the notification was opened by the user from the notification area or not
message: 'My Notification Message', // STRING: The notification message
data: {}, // OBJECT: The push data
}