OneSignal Android NotificationOpenedHandler - 启动活动 [英] OneSignal Android NotificationOpenedHandler - start activity

查看:446
本文介绍了OneSignal Android NotificationOpenedHandler - 启动活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试从Parse迁移到OneSignal,并且在用户点击推送通知后我仍然坚持如何启动新的Activity。我的处理程序正在运行,日志显示文本,问题似乎是如何从我的推送打开处理程序中获取对应用程序上下文的访问权限。示例代码含糊不清, getApplicationContext()在没有先做某事的情况下无效。

Trying to migrate from Parse to OneSignal and I am stuck on how to start a new Activity after the user clicks on a push notification. My handler is working, the log shows the text, the issue seems to be how to gain access to the application context from within my push opened handler. The example code is vague, getApplicationContext() does not work without first doing something else.

我发现的一篇与OneSignal无关的帖子建议扩展Application类以获取对应用程序上下文的访问权限。这不会产生任何语法错误,但我的应用程序崩溃。

One post I came upon, unrelated to OneSignal, suggests extending the Application class to gain access to the application context. This did not produce any syntax errors but my app crashes.

代码:

package com.linkedresponder.onesignal;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.onesignal.OSNotificationAction;
import com.onesignal.OSNotificationOpenResult;
import com.onesignal.OneSignal;

import org.json.JSONObject;

class NotificationOpenHandler extends Application implements OneSignal.NotificationOpenedHandler {
    // This fires when a notification is opened by tapping on it.
    private Context mContext;
    @Override
    public void onCreate() {
        this.mContext = getApplicationContext();
    }
    @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("OneSignalExample", "customkey set with value: " + customKey);
            } else {
                Log.i("OneSignalExample", "No data");
            }
        }

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

        Intent intent = new Intent(mContext, PushClicked.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
         startActivity(intent);
    }
}

错误:

er.onesignal E/AndroidRuntime: FATAL EXCEPTION: main Process: com.linkedresponder.onesignal, PID: 5680
   java.lang.RuntimeException: Unable to start receiver com.onesignal.NotificationOpenedReceiver: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
   at android.app.ActivityThread.handleReceiver(ActivityThread.java:3018)
   at android.app.ActivityThread.-wrap18(ActivityThread.java)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1544)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:154)
   at android.app.ActivityThread.main(ActivityThread.java:6077)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
   at android.content.ComponentName.<init>(ComponentName.java:128)
   at android.content.Intent.<init>(Intent.java:4868)
   at com.linkedresponder.onesignal.NotificationOpenHandler.notificationOpened(NotificationOpenHandler.java:41)
   at com.onesignal.OneSignal.fireNotificationOpenedHandler(OneSignal.java:1009)
   at com.onesignal.OneSignal.runNotificationOpenedCallback(OneSignal.java:954)
   at com.onesignal.OneSignal.handleNotificationOpen(OneSignal.java:1041)
   at com.onesignal.NotificationOpenedProcessor.processIntent(NotificationOpenedProcessor.java:101)
   at com.onesignal.NotificationOpenedProcessor.processFromActivity(NotificationOpenedProcessor.java:57)
   at com.onesignal.NotificationOpenedReceiver.onReceive(NotificationOpenedReceiver.java:11)
   at android.app.ActivityThread.handleReceiver(ActivityThread.java:3011)
   at android.app.ActivityThread.-wrap18(ActivityThread.java) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1544) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:154) 
   at android.app.ActivityThread.main(ActivityThread.java:6077) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 


推荐答案

以下是我的工作方式:

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        OneSignal.startInit(this)
                .setNotificationOpenedHandler(new MyNotificationOpenedHandler(this))
                .init();
    }
}

NotificationOpenedHandler 自定义类

public class MyNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {

    private Application application;

    public MyNotificationOpenedHandler(Application application) {
        this.application = application;
    }

    @Override
    public void notificationOpened(OSNotificationOpenResult result) {

        // Get custom datas from notification
        JSONObject data = result.notification.payload.additionalData;
        if (data != null) {
            String myCustomData = data.optString("key", null);
        }

        // React to button pressed
        OSNotificationAction.ActionType actionType = result.action.type;
        if (actionType == OSNotificationAction.ActionType.ActionTaken)
            Log.i("OneSignalExample", "Button pressed with id: " + result.action.actionID);

        // Launch new activity using Application object
        startApp();
    }

    private void startApp() {
        Intent intent = new Intent(application, MyActivity.class)
                .setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
        application.startActivity(intent);
    }
}

不要忘记将其添加到清单中:

Don't forget to add this to your manifest:

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

这篇关于OneSignal Android NotificationOpenedHandler - 启动活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆