android - 单击通知不会打开 Android 应用程序

标签 android firebase onesignal

我正在使用 onesignal 和 firebase 将通知从 wordpress 博客推送到 Android 应用程序,当我点击刚刚到达的通知时,应用程序只有在后台运行时才会打开。如果它完全关闭,则单击通知将不会执行任何操作。应用未在后台打开,如何实现点击通知打开应用?

下面是处理通知的代码:

 class nyonNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
        // This fires when a notification is opened by tapping on it.
        @Override
        public void notificationOpened(OSNotificationOpenResult result) {

            OSNotificationAction.ActionType actionType = result.action.type;
            JSONObject data = result.notification.payload.additionalData;
            String customKey;

            if (data != null) {
                customKey = data.optString("customkey", null);
                if (customKey != null)
                    Log.i("OneSignalnyon", "customkey set with value: " + customKey);
            }

            if (actionType == OSNotificationAction.ActionType.ActionTaken)
                Log.i("OneSignalnyon", "Button pressed with id: " + result.action.actionID);

            // The following can be used to open an Activity of your choice.
            // Replace - getApplicationContext() - with any Android Context.
            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);

最佳答案

我用来在点击通知时打开应用程序的代码,它工作得很好:

 Intent resultIntent = new Intent(getApplicationContext(), YourActivity.class);
 resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
 //if you want to send some data
 resultIntent.putExtra(AppConstants.NOTIFICATION, data);

现在你必须创建 PendingIntent PendingIntent:根据文档,创建待定 Intent 意味着您授予它执行您指定的操作的权利,就好像另一个应用程序是您自己一样。 https://developer.android.com/reference/android/app/PendingIntent

PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, intent,
                    PendingIntent.FLAG_CANCEL_CURRENT
            );

现在,当您创建通知时,将此待定 Intent setContentIntent(resultPendingIntent) 设置为该通知。

  Notification notification;
    notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
            .setAutoCancel(true)
            .setContentTitle(title)
            .setContentIntent(resultPendingIntent)
            .setStyle(inboxStyle)
            .setWhen(getTimeMilliSec(System.currentTimeMillis() + ""))
            .setSmallIcon(R.drawable.ic_app_icon)
            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), icon))
            .setContentText(message)
            .setChannelId(CHANNEL_ID)
            .build();

关于android - 单击通知不会打开 Android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52488044/

相关文章:

ios - 不显示仅包含数字的iOS推送通知

android - 重复的 ID 布局错误

objective-c - 如何在 OS X Cocoa 应用程序中使用 Google 授权用户

firebase - 未处理的异常:NoSuchMethodError:在空上调用了 'findAncestorStateOfType'方法

ios - Ionic 3 + One Signal handleNotificationReceived 不会被触发

android - 与项目 ':app' 中的依赖项冲突。应用程序 (15.0.2) 和测试应用程序 (12.0.1) 的已解决版本不同(可能是 onesignal&firebase 问题)

android - 在Android中执行进程来读取文件

java - 我的应用程序直接进入 MainActivity

安卓 - java.io.FileNotFoundException

android - Firebase Analytics 未跟踪我的应用屏幕名称 : Screen name (not set)