简体   繁体   English

在 OneSignal 推送通知上打开不同的活动

[英]Opening a different activity on a OneSignal Push Notification

I'm learning how to handle OneSignal Push Notifications on Android devices.我正在学习如何在 Android 设备上处理 OneSignal 推送通知。 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.问题是,当应用程序关闭时,当我收到通知时,即使我定义了必要的功能(我猜)它仍然打开“Splash Activity”,它在清单中定义为 MAIN LAUNCHER。 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.这是我定义 OneSignal 服务的应用程序类。

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.这是我的 CustomNotificationOpening 类。

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.这是我的 NotificationsForPayment 类,它从 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.当应用程序打开时,当我单击通知时,我可以看到“notificationOpened”函数正在触发。 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.任何帮助表示赞赏,非常感谢。

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" .该值应该是"DISABLE" ,而不是"DISABLED"

so, the updated code is:-所以,更新的代码是:-

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

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

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