点击通知无法打开Android应用程序

4

我正在使用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);

你尝试过移除意图标志吗? - fweigl
我刚刚尝试了一下,如果应用程序完全关闭,它仍然无法打开。 - firebone
挂起意图? - hemen
@notTdar 关于PendingIntent怎么样? - firebone
在构建通知时,您是否需要挂起意图来打开活动、服务等? - hemen
@notTdar,它的工作方式如代码所示,当应用程序在后台运行时,通知会被发送并且点击通知会打开应用程序,否则它不会打开应用程序。 - firebone
4个回答

3

我使用的代码可以在点击通知时打开应用,已经完美地运行:

 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:根据文档所述,创建PendingIntent意味着您授予其执行您指定的操作的权限,就像其他应用程序是您自己一样。https://developer.android.com/reference/android/app/PendingIntent

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

现在,当您创建通知时,请将此挂起的意图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();

1
我在使用"PendingIntent.getBroadcast"时犯了错误,实际上我应该使用"PendingIntent.getActivity" :) 我在这里进行了评论,以防有人像我一样遇到问题。 - Saumil Vaghela

1

如果要解决这个问题,首先需要更清晰地阅读文档(这是我没有做的)。以下是文档内容:

默认情况下,当点击通知时,OneSignal会打开或恢复您的启动器活动。您可以通过在AndroidManifest.xml中的应用程序标签内添加meta-data标记com.onesignal.NotificationOpened.DEFAULT,设置为DISABLE来禁用此行为。

请确保在android manifest中注册它,例如:

    <application ...>
       <meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" />
    </application>

创建处理已打开通知的处理程序,例如:

public class MyNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {

    private final Context context;

    public MyNotificationOpenedHandler(Context context) {
        this.context = context;
    }

    @Override
    public void notificationOpened(OSNotificationOpenResult result) {

        if (result.action.type == OSNotificationAction.ActionType.Opened) {
            JSONObject data = result.notification.payload.additionalData;

            if (data == null) {
                return;
            }

            String category = data.optString("category", null);

            if (category == null) {
                return;
            }

            if (category.equals("global")) {
                Intent intent = new Intent(context, NotificationDetailsActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        }
    }
}

下一个要点是:

确保在您的 Application 类的 onCreate 方法中使用 setNotificationOpenedHandler 初始化 OneSignal。您需要从此回调中调用 startActivity

您需要扩展 Application 类,例如:

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        OneSignal.startInit(this)
                .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
                .setNotificationOpenedHandler(new MyNotificationOpenedHandler(getApplicationContext()))
                .unsubscribeWhenNotificationsAreDisabled(true)
                .init();
    }
}

在Android清单文件中设置应用程序名称,例如:
    <application
        android:name=".MyApplication"
        android:icon="@mipmap/ic_launcher"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:theme="@style/AppTheme">

当应用程序关闭时,您可以处理通知。


0

设置 "setNotificationOpenedHandler"

   OneSignal.startInit(this)
            .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
            .setNotificationOpenedHandler(new NotificationOpenedHandler())
            .init();

将此类添加到您的启动器活动中(确保通知具有“additionalData”)

 public class NotificationOpenedHandler 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("Data", null);
            if (customKey != null)
            {
                Log.d("LOGGED", "notificationOpened: " +  customKey);
                if(customKey.equals("Notification"))
                {
                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
                else
                {
                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
                //Toast.makeText(MainActivity.this, "Value is : " + customKey, Toast.LENGTH_SHORT).show();
            }
        }

更多信息 https://documentation.onesignal.com/docs/android-native-sdk#section--notificationopenedhandler-


回答问题前请先阅读问题。我使用了文档中的代码,而你只是复制粘贴了同样的内容。 - firebone
据我记得当我发布这篇文章时,没有涉及到这段代码片段。不知道,可能是我疏忽了。没关系。 :) 但是对于我的端来说,这是有效的。 - Samin

-1
请将以下内容添加到您的AndroidManifest.xml中,以防止启动主Activity。
 <application ...>
      <meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" />
    </application>

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