【问题标题】:How to know if the app is open from the android notification tray?如何知道应用程序是否从 android 通知托盘打开?
【发布时间】:2018-10-01 18:51:31
【问题描述】:

如何知道应用程序是否从 android 通知托盘打开?例如,我已经关闭了应用程序(从最近的应用程序列表中清除)。但我收到来自后端 websocket 的通知,我按下它,它打开了应用程序。所以我的问题是,有没有办法检查这是否从通知中打开?

【问题讨论】:

  • 我从未使用过本机反应,但本能会在创建意图时使用 putExtra() 并检查它。 stackoverflow.com/questions/39351650/…
  • 你能告诉我你配置 react-native-push-notification 的 sn-p
  • 你试过我的解决方案了吗?

标签: android react-native react-native-push-notification


【解决方案1】:

很简单,您可以在推送通知侦听器中接收通知负载

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
}

【讨论】:

  • 你好,谢谢你的建议。是的,我正在使用这个库。事实证明,每当应用程序未启动时,我都必须将其放在 App.js 中。在此之前,我在应用程序的主页中注册了推送通知(在登录并授予凭据之后)。但是,我仍然持怀疑态度,对这种解决方法不太满意。
  • 你为什么怀疑,这应该是如何实现的,我们已经实现了和你一样的监听器。顺便说一句,如果答案对您有所帮助,那将是一种善意的姿态。谢谢
【解决方案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
        }
    }

否则你可以使用 Native Module 方法:

当您设置传递给通知.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
    }
}

附加信息: Handling intents Creating Intents

【讨论】:

  • 他正在使用本机反应。他和 PendingIntent 之间有一两层间接关系
  • 没错,但据我所知,您必须在 native module 中这样做。如果我错了,请纠正我
  • 是的,但是他正在使用一个特定的库来为他做本地模块。这是本机代码中的正确答案。但对他来说,答案要么是使用图书馆来实现这一目标,要么他必须扔掉图书馆并自己做。
  • 我不知道他必须采取的所有步骤,但我知道这一点。随意使用我的答案来提供整个方式。
  • @GabeSechan 我添加了更多信息,有帮助吗?
猜你喜欢
  • 1970-01-01
  • 2022-12-21
  • 2017-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多