0

当用户点击推送通知时,以下代码充当我的处理程序。该代码工作正常,并launchURL准确记录在我的日志中。但是,作为下一步,我想将用户引导到一个名为ViewPushLink. 我试图遵循 SO 上其他地方的类似说明,但作为新手,我将不胜感激一些具体的帮助。

class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
     public static String launchURL;

    @Override
    public void notificationOpened(OSNotificationOpenResult result) {
        OSNotificationAction.ActionType actionType = result.action.type;
        JSONObject data = result.notification.payload.additionalData;

        if (data != null) {
            launchURL = data.optString("launchURL");
            if (launchURL != null) {
                Log.i("OneSignalExample", "launchURL value: " + launchURL);

            }
        }

        }

}

编辑:ExampleNotificationOpenedHandler从我的主要活动中调用,如下所示:

public class MainActivity extends AppCompatActivity {


       private ProgressDialog progress;

    public static boolean isNetworkStatusAvailable (Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null)
        {
            NetworkInfo netInfos = connectivityManager.getActiveNetworkInfo();
            if(netInfos != null)
                if(netInfos.isConnected())
                    return true;
        }
        return false;
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
        if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        } else setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        // OneSignal Initialization
        OneSignal.startInit(this)
                .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
                .unsubscribeWhenNotificationsAreDisabled(true)
                .setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
                .init();
4

2 回答 2

0

将您的活动更改为此;当然它会帮助你。

class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
     public static String launchURL;

    @Override
    public void notificationOpened(OSNotificationOpenResult result) {
        OSNotificationAction.ActionType actionType = result.action.type;
        JSONObject data = result.notification.payload.additionalData;

        if (data != null) {
            launchURL = data.optString("launchURL");
            if (launchURL != null) {
                Log.i("OneSignalExample", "launchURL value: " + launchURL);
                Intent intent = new Intent(getApplicationContext, ViewPushLink.class)
                startActivity(intent);
            }
        }

        }

}
于 2018-10-26T17:19:43.740 回答
0

将您的代码放在项目的应用程序类中。
对于使用 OneSignal SDK 处理推送通知,需要考虑三件事。{如果您的项目有多个 Fragment 和一个 Main Activity(Navigation Drawer Activity),其余的其他活动从 MainActivity 的 Fragment 中调用}

  1. 目标活动
  2. 相关操作,例如,您的活动的哪个部分,您要打开哪个片段,通过您可以在 jsonObject 数据中找到的一些操作来区分每个
  3. Intent Flags,一个你想打开一个Activity的例子,它的实例已经在backStack中了,所以你可能想清空返回栈,打开新的activity实例,也可能打开同一个实例。

例子:

public XYZApplication extends Application{

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

        if (data != null) {
            String target = data.optString("target");
            String action = "";
            switch (target) {
                case "xyz":
                    action = data.optString("action");
                    Utils.setTarget(target);
                    if (action.equalsIgnoreCase("xyz")
                            || action.equalsIgnoreCase("abc")) {
                        Intent xyzIntent = new Intent(getApplicationContext(), XYZActivity.class);
                        homeIntent.putExtra("action_key", "action_xyz");
                        homeIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(xyzIntent);
}

有关更多详细信息,请参阅一个信号 SDK 文档 https://documentation.onesignal.com/docs

于 2018-10-26T18:09:50.760 回答