[英]Launch share intent on Onesignal notification action button click
I am able to successfully send notifications in onesignal. 我能够在onesignal中成功发送通知。 All of the features are working fine.
所有功能均正常运行。 But today when I tried to implement action buttons to the notifications, its not working.
但是今天,当我尝试对通知实施操作按钮时,它不起作用。 I tried to add an action button with id "ActionOne", on click of this action button below notification, it should start a share intent to share the text which i want.
我试图添加一个ID为“ ActionOne”的操作按钮,单击通知下方的此操作按钮,它应该启动共享意图以共享我想要的文本。 But each time I do that the app crashes.
但是每次我这样做,应用程序都会崩溃。 However when I tried to add a simple toast to another action button with id "ActionTwo", it worked.
但是,当我尝试向ID为“ ActionTwo”的另一个操作按钮添加简单的吐司时,它就起作用了。 Please help me out of this.
请帮助我。 Here is the code -
这是代码-
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();
}
}
}
}
Here is the logcat when notification is received - 这是收到通知时的日志-
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)
Either 要么
cache the Context object via constructor in your adapter, or 通过适配器中的构造函数缓存Context对象,或者
get it from your view. 从您的角度来看。
Or as a last resort, 或者作为最后的手段,
add - FLAG_ACTIVITY_NEW_TASK flag to your intent: 添加-FLAG_ACTIVITY_NEW_TASK标志到您的意图:
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Edit - i would avoid setting flags as it will interfere with normal flow of event and history stack. 编辑-我会避免设置标志,因为它会干扰事件和历史记录堆栈的正常流程。
Finally after a lot of struggle i got the solution :) 经过很多努力,终于得到了解决方案:)
Here's the final code of sharing intent - 这是共享意图的最终代码-
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.