点击通知无法打开Android应用

时间:2018-09-24 22:12:40

标签: 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);

4 个答案:

答案 0 :(得分:0)

将以下内容添加到您的AndroidManifest.xml中,以防止启动您的主活动

adb debugging

答案 1 :(得分: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-

答案 2 :(得分:0)

我在点击通知时用于打开应用程序的代码,效果很好:

 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
            );

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

答案 3 :(得分:0)

要解决此问题,第一件事是更清楚地阅读文档(我没有做的事情),所以这里是:

  

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

请确保将其注册在android清单中,例如:

    <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">

并且您已经准备好在应用关闭时处理通知。