如何判断Android通知栏中的应用程序是否已打开?

3

如何判断 Android 通知栏中的应用程序是否已打开?例如,我已经关闭了该应用程序(从最近使用的应用程序列表中清除)。但是,我从后端 WebSocket 收到通知,我按下它,它会打开该应用程序。所以我的问题是,有没有办法检查是否是从通知打开的应用程序?


我从未使用过React Native,但我的直觉是在创建意图时使用putExtra()并进行检查。https://dev59.com/XFkS5IYBdhLWcg3w456B - Bakon Jarser
你能给我展示一下你配置 react-native-push-notification 的代码片段吗? - Haider Ali
你试过我的解决方案了吗? - Haider Ali
2个回答

3

查看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
        }
    }

否则,您可以使用本地模块的方法:
当您设置传递给通知的.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
    }
}

附加信息: 处理意图 创建意图


他正在使用React Native。在他和PendingIntent之间有一两个间接层级。 - Gabe Sechan
真的,但据我所知,你必须在本地模块中完成。如果我错了,请纠正我。 - leonardkraemer
是的,但他正在使用特定的库来为他执行本地模块。这是在本地代码中的正确答案。但对他来说,答案要么是使用该库使其发生的方法,要么就是必须放弃该库并自己完成。 - Gabe Sechan
我不知道他在中间要采取哪些步骤,但我知道这一点。随意使用我的答案来提供整个过程。 - leonardkraemer
@GabeSechan 我增加了一些信息,这有帮助吗? - leonardkraemer

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
}

嗨,感谢你的建议。是的,我正在使用这个库。结果发现,每当应用程序没有启动时,我必须将它放在App.js中。在此之前,我在应用程序的主页(登录并授予权限后)注册了推送通知。但是,我仍然持怀疑态度,并不太满意这样的解决方法。 - Fuji
你为什么持怀疑态度,这是需要实现的方式,我们已经按照你的做法实现了监听器。顺便说一句,如果答案对你有帮助,一个赞会是一种友善的表示。谢谢。 - Haider Ali

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接