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

Posted

技术标签:

【中文标题】如何使用 OneSignal 打开特定活动?【英文标题】:How to open a specific activity using OneSignal? 【发布时间】:2018-07-24 22:26:01 【问题描述】:

我正在使用 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】:

这将是您的 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 之后添加另一个else if,即可继续添加更多要启动的密钥和活动。

如果您要使用所有这些意图,请确保在您的 AndroidManifest.xml 中没有为您将使用通知启动的活动设置任何 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">

此代码中用于通知工作的唯一强制性部分是android:name=".ApplicationClass"

希望这会有所帮助。

【讨论】:

以上是关于如何使用 OneSignal 打开特定活动?的主要内容,如果未能解决你的问题,请参考以下文章

OneSignal如何向ionic2中的特定用户发送推送通知

java Onesignal:打开通知时恢复上次活动

如何在颤振应用程序中无上下文导航?

Onesignal,Ionic App:单击通知时打开特定视图不起作用

Swift - 如何从 OneSignal SDK 向特定用户名标签发送发布通知?

在 Android 中点击 One Signal 推送通知时如何导航到特定页面?