[英]how to know that an app was launch from a click to a notification in the system tray?
[英]How to know if the app is open from the android notification tray?
如何知道應用程序是否從Android通知托盤中打開? 例如,我已關閉應用程序(從最近的應用程序列表中清除)。 但我從后端websocket收到通知,我按下它,它打開應用程序。 所以我的問題是,有沒有辦法檢查這是否通知?
查看react-native-push-notification的來源 +接下來的50行(最多為setContentIntent
),您可以在intent中檢查額外的“通知”。
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
}
}
它很簡單,您可以在推送通知監聽器中收到通知負載
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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.