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
Just add another else if after the ones already included to go on adding more keys and activities to launch. If you're going to use all these intents, make sure to have not have anything setup for android:launchMode in your AndroidManifest. xml for the activities that you're going to launch using the notifications.
Click on the + Add Action. On the right side, search and select the Trigger Push Notification action. To set who should receive the push notification, set the Audience to Single Recipient or Multiple Recipient.
Google then has an API to send messages to applications through this connection with the FCM API. Due to this requirement, OneSignal itself uses the FCM API internally to send messages to Android devices.
I just missed to build constructor in class before 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
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
OneSignal.startInit(this)
.setNotificationOpenedHandler(new ExampleNotificationOpenedHandler(this))
.init();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With