如何知道应用程序是否已从 android 通知栏打开?
How to know if the app is open from the android notification tray?
如何知道应用程序是否已从 android 通知栏打开?例如,我关闭了应用程序(从最近的应用程序列表中清除)。但我收到来自后端 websocket 的通知,我按下它,它打开了应用程序。所以我的问题是,有没有办法通过通知检查这是否已打开?
查看 react-native-push-notification 的来源 + 接下来的 50 行(最多 setContentIntent
),您可以检查意图中的 "notification" 额外内容。
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
}
如何知道应用程序是否已从 android 通知栏打开?例如,我关闭了应用程序(从最近的应用程序列表中清除)。但我收到来自后端 websocket 的通知,我按下它,它打开了应用程序。所以我的问题是,有没有办法通过通知检查这是否已打开?
查看 react-native-push-notification 的来源 + 接下来的 50 行(最多 setContentIntent
),您可以检查意图中的 "notification" 额外内容。
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
}