OneSignal 推送通知点击打开 activity
OneSignal Push Notification Click to open activity
我集成了一个推送通知信号库。我想通过点击推送通知打开一个特定的 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
这意味着 context
变量为空。
如果您使用已选择的构造函数查看 Intent 的源代码
public Intent(Context packageContext, Class<?> cls) {
mComponent = new ComponentName(packageContext, cls);
}
和ComponentName
构造函数
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;
}
您可以使用 DI 提供上下文,例如 Dagger2。
或实施应用class
public class DemoApp extends Application {
private static DemoApp instance;
@Override
public void onCreate() {
instance = this;
super.onCreate();
}
public static DemoApp getInstance() {
return instance;
}
}
并且在您的清单中
<application
android:name=".demo.DemoApp"
我只是错过了在 onReceivedMethod
之前在 class 中构建构造函数
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);
}
并在应用程序中传递上下文 class
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
OneSignal.startInit(this)
.setNotificationOpenedHandler(new ExampleNotificationOpenedHandler(this))
.init();
}
我集成了一个推送通知信号库。我想通过点击推送通知打开一个特定的 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
这意味着 context
变量为空。
如果您使用已选择的构造函数查看 Intent 的源代码
public Intent(Context packageContext, Class<?> cls) {
mComponent = new ComponentName(packageContext, cls);
}
和ComponentName
构造函数
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;
}
您可以使用 DI 提供上下文,例如 Dagger2。
或实施应用class
public class DemoApp extends Application {
private static DemoApp instance;
@Override
public void onCreate() {
instance = this;
super.onCreate();
}
public static DemoApp getInstance() {
return instance;
}
}
并且在您的清单中
<application
android:name=".demo.DemoApp"
我只是错过了在 onReceivedMethod
之前在 class 中构建构造函数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);
}
并在应用程序中传递上下文 class
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
OneSignal.startInit(this)
.setNotificationOpenedHandler(new ExampleNotificationOpenedHandler(this))
.init();
}