0

我能够在 onesignal 中成功发送通知。所有功能都运行良好。但是今天当我尝试对通知实现操作按钮时,它不起作用。我尝试添加一个 ID 为“ActionOne”的操作按钮,单击通知下方的此操作按钮,它应该启动一个共享意图来共享我想要的文本。但是每次我这样做时,应用程序都会崩溃。但是,当我尝试将简单的 toast 添加到另一个 ID 为“ActionTwo”的操作按钮时,它起作用了。请帮我解决这个问题。这是代码 -

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

    if (data != null) {
        open = data.optString("open", null);

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

    }

    if (actionType == OSNotificationAction.ActionType.ActionTaken){
        Log.i("OneSignalExample", "Button pressed with id: " + result.action.actionID);


        if (result.action.actionID.equals("ActionOne")) {

            Intent sharingIntent = new Intent(Intent.ACTION_SEND);
            sharingIntent.setType("text/plain");
            String shareBodyText = "hello";
            sharingIntent.putExtra(Intent.EXTRA_SUBJECT,"");
            sharingIntent.putExtra(Intent.EXTRA_TEXT, shareBodyText);
            MyApplication.getContext().startActivity(Intent.createChooser(sharingIntent, "Share via"));

        }else if (result.action.actionID.equals("ActionTwo")) {
            Toast.makeText(MyApplication.getContext(), "ActionTwo Button was pressed", Toast.LENGTH_LONG).show();
        }
      }
   }
}

这是收到通知时的 logcat -

11-16 18:57:47.266 26067-26067/com.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                             Process: com.myapplication, PID: 26067
                                                             java.lang.RuntimeException: Unable to start receiver com.onesignal.NotificationOpenedReceiver: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
                                                                 at android.app.ActivityThread.handleReceiver(ActivityThread.java:2851)
                                                                 at android.app.ActivityThread.access$1700(ActivityThread.java:178)
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1547)
                                                                 at android.os.Handler.dispatchMessage(Handler.java:111)
                                                                 at android.os.Looper.loop(Looper.java:194)
                                                                 at android.app.ActivityThread.main(ActivityThread.java:5653)
                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                 at java.lang.reflect.Method.invoke(Method.java:372)
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                              Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
                                                                 at android.app.ContextImpl.startActivity(ContextImpl.java:1372)
                                                                 at android.app.ContextImpl.startActivity(ContextImpl.java:1359)
                                                                 at android.content.ContextWrapper.startActivity(ContextWrapper.java:323)
                                                                 at com.myapplication.MyNotificationOpenedHandler.notificationOpened(MyNotificationOpenedHandler.java:52)
                                                                 at com.onesignal.OneSignal$15.run(OneSignal.java:1486)
                                                                 at com.onesignal.OSUtils.runOnMainUIThread(OSUtils.java:204)
                                                                 at com.onesignal.OneSignal.fireNotificationOpenedHandler(OneSignal.java:1483)
                                                                 at com.onesignal.OneSignal.runNotificationOpenedCallback(OneSignal.java:1432)
                                                                 at com.onesignal.OneSignal.handleNotificationOpen(OneSignal.java:1519)
                                                                 at com.onesignal.NotificationOpenedProcessor.processIntent(NotificationOpenedProcessor.java:116)
                                                                 at com.onesignal.NotificationOpenedProcessor.processFromContext(NotificationOpenedProcessor.java:51)
                                                                 at com.onesignal.NotificationOpenedReceiver.onReceive(NotificationOpenedReceiver.java:38)
                                                                 at android.app.ActivityThread.handleReceiver(ActivityThread.java:2844)
                                                                 at android.app.ActivityThread.access$1700(ActivityThread.java:178) 
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1547) 
                                                                 at android.os.Handler.dispatchMessage(Handler.java:111) 
                                                                 at android.os.Looper.loop(Looper.java:194) 
                                                                 at android.app.ActivityThread.main(ActivityThread.java:5653) 
                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                 at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960) 
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
4

2 回答 2

0

任何一个

通过适配器中的构造函数缓存 Context 对象,或者

从你的角度得到它。

或者作为最后的手段,

在您的意图中添加 - FLAG_ACTIVITY_NEW_TASK 标志:

myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

编辑 - 我会避免设置标志,因为它会干扰事件和历史堆栈的正常流动。

于 2017-11-16T13:52:25.553 回答
0

经过一番努力,我终于找到了解决方案:)

这是共享意图的最终代码 -

Log.i("OneSignalExample", "button id called: " + result.action.actionID);
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBodyText = "hello";
sharingIntent.setFlags(FLAG_ACTIVITY_NEW_TASK);
sharingIntent.putExtra(Intent.EXTRA_SUBJECT,"share");
sharingIntent.putExtra(Intent.EXTRA_TEXT, shareBodyText);
Intent share = Intent.createChooser(sharingIntent, "share via");
share.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MyApplication.getContext().startActivity(share);
于 2017-11-16T14:44:53.620 回答