Furkan Yurdakul
Furkan Yurdakul

Reputation: 3167

Opening a different activity on a OneSignal Push Notification

I'm learning how to handle OneSignal Push Notifications on Android devices. The problem is, while the application is closed, when I receive a notification, even though I defined the neccessary functions (I guess) it still opens the "Splash Activity", which is defined as MAIN LAUNCHER in manifest. I need to open a different activity with the payload data in it. The link I've referenced from creating these codes is this and I've seen the reference on this answer. I'm only showing the relevant code since this project is classified.

Here is my Manifest file.

<application
        android:name="packageName.CustomAppName"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data android:name="com.onesignal.NotificationOpened.DEFAULT"
            android:value="DISABLED"/>
        <activity
            android:name="anotherPackageName.SplashActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name="anotherPackageName.PaymentActivity"
            android:screenOrientation="portrait" />
        <service
            android:name="somepackagename.NotificationsForPayment"
            android:exported="false"
            android:permission="android.permission.BIND_JOB_SERVICE">
            <intent-filter>
                <action android:name="com.onesignal.NotificationExtender" />
            </intent-filter>
        </service>
</application>

Here is my application class where I define OneSignal service.

public class CustomAppName extends Application {
    private static CustomAppName instance;
    public static CustomAppName getInstance() {
        return instance;
    }
    public void onCreate() {
        super.onCreate();
        OneSignal.startInit(this)
                .setNotificationOpenedHandler(new CustomNotificationOpening())
                .init();
        instance = this;
    }
}

Here is my CustomNotificationOpening class.

public class CustomNotificationOpening implements OneSignal.NotificationOpenedHandler {

    @Override
    public void notificationOpened(OSNotificationOpenResult notification) {
        notification.notification.payload.additionalData.names();
        JSONObject data = notification.notification.payload.additionalData;

        Intent intent = new Intent(CustomAppName.getInstance(), PaymentActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("paymentModel", data);
        CustomAppName.getInstance().startActivity(intent);
}

And here is my NotificationsForPayment class which extends from NotificationExtenderService.

public class NotificationsForPayment extends NotificationExtenderService {
    @Override
    protected boolean onNotificationProcessing(OSNotificationReceivedResult notification) {
        NotificationExtenderService.OverrideSettings overrideSettings = new NotificationExtenderService.OverrideSettings();
        overrideSettings.extender = new NotificationCompat.Extender() {
            @Override
            public NotificationCompat.Builder extend(NotificationCompat.Builder builder) {
                // Sets the background notification color to Red on Android 5.0+ devices.
                Bitmap icon = BitmapFactory.decodeResource(CustomAppName.getInstance().getResources(),
                        R.drawable.ic_os_notification_fallback_white_24dp);
                builder.setLargeIcon(icon);
                return builder.setColor(new BigInteger("FF0000FF", 16).intValue());
            }
        };
    OSNotificationDisplayedResult displayedResult = displayNotification(overrideSettings);
}

I don't really know where I'm doing wrong. While the application is open, when I click the notification I can see that "notificationOpened" function is firing. But when it's closed, since I can't debug the program and the notification opens the splash activity, I knew the time for me was to ask this question because none of the answers I've found worked. Is there any way to open the other activity with specific data from the notification while the application was closed? Any help is appreciated, thank you so much.

Upvotes: 1

Views: 2577

Answers (1)

Furkan Yurdakul
Furkan Yurdakul

Reputation: 3167

Found where the error was.

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

The value was supposed to be "DISABLE", not "DISABLED".

so, the updated code is:-

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

Upvotes: 2

Related Questions