在Onesignal通知操作按钮上单击启动共享意图

时间:2017-11-16 08:15:28

标签: android push-notification android-notifications onesignal share-intent

我能够在信号中成功发送通知。所有功能都运行正常。但是今天当我试图对通知实施动作按钮时,它无法正常工作。我尝试添加一个ID为“ActionOne”的动作按钮,点击通知下方的此动作按钮,它应该启动共享意图以共享我想要的文本。但每次我这样做应用程序崩溃。然而,当我尝试将一个简单的吐司添加到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) 

2 个答案:

答案 0 :(得分:0)

无论

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

从你的观点中得到它。

或作为最后的手段,

添加 - FLAG_ACTIVITY_NEW_TASK标记到您的意图:

myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

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

答案 1 :(得分: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);