如何知道应用程序是否从 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
}
}
否则,您可以使用原生模块方法:
当您设置传递给通知.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
}
}
关于android - 如何知道应用程序是否从 android 通知托盘打开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52597193/