简体   繁体   English

OneSignal 推送通知 点击打开活动

[英]OneSignal Push Notification Click to open activity

I have integrated one signal library for push notification.我已经集成了一个用于推送通知的信号库。 I want to open a particular activity from click of push notification while app is not running I am receiving push notification but while I am clicking on notification the app crashes.我想在应用程序未运行时通过点击推送通知打开特定活动 我收到推送通知但当我点击通知时应用程序崩溃。 Here is my code for notification receiver这是我的通知接收器代码

public class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler 
{

Context context;
@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.e("OneSignalExample", "customkey set with value: " + customKey);
    }

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

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

here is my error msg这是我的错误消息

Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference

I just missed to build constructor in class before onReceivedMethod我只是错过了在 onReceivedMethod 之前在课堂上构建构造函数

Context context2;

ExampleNotificationOpenedHandler(Context context) {
    context2 = context;
}

@Override
public void notificationOpened(OSNotificationOpenResult result) {
    OSNotificationAction.ActionType actionType = result.action.type;
    JSONObject data = result.notification.payload.additionalData;
    String customKey;

    Intent intent = new Intent(context2,User_Detail.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
    context2.startActivity(intent);


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

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


    }

and also pass context in Application class并在 Application 类中传递上下文

  @Override
public void onCreate() {
    super.onCreate();
    mInstance = this;


    OneSignal.startInit(this)
            .setNotificationOpenedHandler(new ExampleNotificationOpenedHandler(this))
            .init();

}

This means that context variable is null.这意味着context变量为空。

If you look in source code of Intent with picked construcor如果您使用选择的构造函数查看 Intent 的源代码

public Intent(Context packageContext, Class<?> cls) {
    mComponent = new ComponentName(packageContext, cls);
}

And ComponentName construcorComponentName构造函数

public ComponentName(Context pkg, String cls) {
     if (cls == null) throw new NullPointerException("class name is null");
     mPackage = pkg.getPackageName(); //here you get crash
     mClass = cls;
 }

You can use DI to provide context, for example Dagger2.您可以使用 DI 来提供上下文,例如 Dagger2。

Or implement application class或者实现应用类

public class DemoApp extends Application {

    private static DemoApp instance;

    @Override
    public void onCreate() {
        instance = this;
        super.onCreate();
    }

    public static DemoApp getInstance() {
        return instance;
    }

}

And in your manifest在你的清单中

<application
        android:name=".demo.DemoApp"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM