gpt4 book ai didi

android - OneSignal 推送通知点击打开 Activity

转载 作者:太空宇宙 更新时间:2023-11-03 12:46:29 27 4
gpt4 key购买 nike

我已经为推送通知集成了一个信号库。我想在应用程序未运行时通过单击推送通知打开特定 Activity 我收到推送通知,但当我点击通知时,应用程序崩溃了。这是我的通知接收器代码

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

这是我的错误信息

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

最佳答案

我只是错过了在 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);


}

并在 Application 类中传递上下文

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


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

}

关于android - OneSignal 推送通知点击打开 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43014512/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com