如何使用OneSignal打开特定活动?

时间:2018-07-24 22:26:01

标签: java android android-studio push-notification onesignal

我正在使用onesignal发送推送通知。 当用户单击推送通知时,我使用此示例代码打开了特定的活动。 如果我想打开其他特定活动,该怎么办?

 package com.moho.app;

    import android.content.Intent;
    import android.util.Log;
    import android.widget.Toast;

    import com.onesignal.OSNotificationAction;
    import com.onesignal.OSNotificationOpenResult;
    import com.onesignal.OneSignal;

    import org.json.JSONObject;


    public class MyNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
        // This fires when a notification is opened by tapping on it.
        @Override
        public void notificationOpened(OSNotificationOpenResult result) {
            OSNotificationAction.ActionType actionType = result.action.type;
            JSONObject data = result.notification.payload.additionalData;
            String activityToBeOpened;
            String activity;

            //While sending a Push notification from OneSignal dashboard
            // you can send an addtional data named "activityToBeOpened" and retrieve the value of it and do necessary operation
            //If key is "activityToBeOpened" and value is "AnotherActivity", then when a user clicks
            //on the notification, AnotherActivity will be opened.
            //Else, if we have not set any additional data MainActivity is opened.
            if (data != null) {
                activityToBeOpened = data.optString("activityToBeOpened", null);
                if (activityToBeOpened != null && activityToBeOpened.equals("AnotherActivity")) {
                    Log.i("OneSignalExample", "customkey set with value: " + activityToBeOpened);
                    Intent intent = new Intent(MainMenu.getContext(), AboutUs.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
                    MainMenu.getContext().startActivity(intent);

                } else if (activityToBeOpened != null && activityToBeOpened.equals("MainActivity")) {
                    Log.i("OneSignalExample", "customkey set with value: " + activityToBeOpened);
                    Intent intent = new Intent(MainMenu.getContext(), MainMenu.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
                    MainMenu.getContext().startActivity(intent);
                } else {
                    Intent intent = new Intent(MainMenu.getContext(), MainMenu.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
                    MainMenu.getContext().startActivity(intent);
                }

            }

    }

1 个答案:

答案 0 :(得分:1)

这将是您的ApplicationClass.java的代码:

package com.application;

import android.app.Application;
import android.content.Intent;
import android.util.Log;
import com.onesignal.OSNotificationAction;
import com.onesignal.OSNotificationOpenResult;
import com.onesignal.OneSignal;
import org.json.JSONObject;

@SuppressWarnings("unused")

public class ApplicationClass extends Application
{
    @Override
    public void onCreate()
    {
        super.onCreate();

        OneSignal.startInit(this)
                .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
                .setNotificationOpenedHandler(new NotificationOpenedHandler())
                .unsubscribeWhenNotificationsAreDisabled(true)
                .init();
    }

    public class NotificationOpenedHandler implements OneSignal.NotificationOpenedHandler
    {
        @Override
        public void notificationOpened(OSNotificationOpenResult result)
        {
            OSNotificationAction.ActionType actionType = result.action.type;
            JSONObject data = result.notification.payload.additionalData;
            String activityToBeOpened;
            String activity;

            if (data != null)
            {
                activityToBeOpened = data.optString("activityToBeOpened", null);
                if (activityToBeOpened != null && activityToBeOpened.equals("ABC"))
                {
                    Log.i("OneSignal", "customkey set with value: " + activityToBeOpened);
                    Intent intent = new Intent(ApplicationClass.this, ABCActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
                    startActivity(intent);
                } else if (activityToBeOpened != null && activityToBeOpened.equals("DEF"))
                {
                    Intent intent = new Intent(ApplicationClass.this, DEFActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
                    startActivity(intent);
                }
            }
        }
    }
}

根据您的需要定制代码(更改数据值和类的名称)。

我试图设置一个空的附加数据来打开MainActivity,但是,在我的情况下,此操作不起作用。可能是因为我定义了:

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

。因此,我想,如果需要,您还必须设置另一个附加数据值来打开MainActivity。只需在已经包含的else if之后添加另一个android:launchMode,即可继续添加更多要启动的键和活动。

如果要使用所有这些意图,请确保在AndroidManifest.xml中没有为<application android:name=".ApplicationClass" android:allowBackup="true" android:fullBackupContent="@xml/backup_descriptor" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme" tools:ignore="AllowBackup"> 进行任何设置,以准备使用通知启动的活动。基本上,它不应该存在。

此外,要使此功能正常运行,您必须将AndroidManifest.xml更改为以下内容:

android:name=".ApplicationClass"

此代码中用于通知正常工作的唯一必需部分是self.lblTitle.backgroundColor = Colors.clear self.lblTitle.borderColor = Colors.clear

希望这会有所帮助。