如何知道是否从android通知托盘中打开了该应用程序?例如,我已经关闭了该应用程序(已从最近的应用程序列表中清除)。但是我从后端Websocket收到通知,我按了它,它打开了应用程序。所以我的问题是,有没有办法检查通知是否打开?
答案 0 :(得分:2)
查看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
}
}
否则,您可以使用本机模块方法:
设置PendingIntent
并将其传递到通知.setContentIntent()
方法时,请指定一个操作,然后在应用程序中进行恢复。通知示例:
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
}
}
答案 1 :(得分:2)
很简单,您会在推送通知侦听器中收到通知有效载荷
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
}