从通知栏跳转到Activity,再跳转至Fragment
2017-05-23 15:10
435 查看
使用极光推送。
收到推送,发出通知。
部分代码
Activity收到通知
Fragment接受数据
注:
地址:https://stackoverflow.com/questions/6352281/getintent-extras-always-null
The method getIntent() returns the FIRST intent than launch activity.
So, when the activity is CLOSED (terminated) and the user clicks on the notification, it will run a new instance of the activity and getIntent() works as expected (Extras is not null).
But if the activity is “sleeping” (it is in the background) and the user clicks on the notification, getIntent() always returns the very FIRST intent that started the activity and NOT the notification intent.
So to catch the notification intent while the application is running, simply use this
翻译:
因为 getIntent()方法返回首个intent在加载acitivity时.
所以,当activity关闭或者中止时,用户点击通知,会重新创建一个activity,而getIntent按按照如期进行工作,值为null。
但是,如果activity是休眠的(在后台运行)然后用户点击通知,getIntent总是返回第一个intent,同时开启activity,但并不是通知的那个intent。
为了在app运行时,抓住通知中的intent,简单使用如下的代码。
收到推送,发出通知。
部分代码
if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) { Log.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: Key" + bundle.getString("cn.jpush.android.MSG_ID") ); //processCustomMessage(context, bundle); int id=(int)((Math.random()*9+1)*100000); Msg msg = JSON.parseObject(bundle.getString(JPushInterface.EXTRA_MESSAGE), Msg.class); if (msg.isNeedSpeek()) { speak(msg.getContent()); } Intent intent1=null; if(msg.getMsgType()==1){ intent1=new Intent(context, OrderDetailActivity.class); intent1.putExtra("page","tuisongtongzhi"); intent1.putExtra("orderId",msg.getField1()); }else if(msg.getMsgType()==2){ intent1 = new Intent(context,OrderDetailActivity.class); intent1.putExtra("page","xitongtongzhi"); }else if(msg.getMsgType()==3){ intent1=new Intent(context,ContactsMsgActivity.class); }else if(msg.getMsgType()==4){ intent1=new Intent(context, MainActivity.class); intent1.putExtra("msg",4); }else if(msg.getMsgType()==5){ intent1=new Intent(context, MainActivity.class); intent1.putExtra("msg",5); } //当发出多个通知时,需要区分requestCode,不然会认为是同一个notification,于是就不再更新参数...... PendingIntent pendingIntent = PendingIntent.getActivity(context, id, intent1, 0); // NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context); mBuilder.setContentTitle(msg.getTitle()) .setContentText(msg.getContent()) .setContentIntent(pendingIntent) .setNumber(number) .setTicker(msg.getContent()) .setWhen(System.currentTimeMillis()) .setPriority(Notification.PRIORITY_HIGH) .setAutoCancel(true) .setOngoing(false) .setDefaults(Notification.DEFAULT_VIBRATE) .setSmallIcon(R.mipmap.ic_launcher); mNotificationManager.notify(id, mBuilder.build());
Activity收到通知
@Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); Log.e("TAG","onNewIntent"); if (intent != null && intent.getExtras() != null) { //跳转到联系人页面 if (intent.getExtras().getInt("msg") == 4||intent.getExtras().getInt("msg") == 5||intent.getExtras().getString("page").equals("MsgActivity")) { contactListFragment = new ContactListFragment(); Fragment fragment = contactListFragment; transaction = fragmentManager.beginTransaction(); transaction.replace(R.id.content_fl, fragment); transaction.commit(); onBottomClick(contacts_fl); } getIntent().putExtras(intent);//发送数据到fragment } } 注:activity必须设置launchmode为singletask
Fragment接受数据
//接受数据来判断显示是联系人还是商户 if(getActivity().getIntent().getExtras()!=null){ if(getActivity().getIntent().getExtras().get("page").equals("MsgActivity")||getActivity().getIntent().getExtras().get("msg").equals(4)){ listView.setVisibility(View.GONE); shanghu_lv.setVisibility(View.VISIBLE); yonghu_line.setVisibility(View.VISIBLE); lianxiren_line.setVisibility(View.GONE); contactListLayout.setShowSiderBar(false); } }
注:
地址:https://stackoverflow.com/questions/6352281/getintent-extras-always-null
The method getIntent() returns the FIRST intent than launch activity.
So, when the activity is CLOSED (terminated) and the user clicks on the notification, it will run a new instance of the activity and getIntent() works as expected (Extras is not null).
But if the activity is “sleeping” (it is in the background) and the user clicks on the notification, getIntent() always returns the very FIRST intent that started the activity and NOT the notification intent.
So to catch the notification intent while the application is running, simply use this
翻译:
因为 getIntent()方法返回首个intent在加载acitivity时.
所以,当activity关闭或者中止时,用户点击通知,会重新创建一个activity,而getIntent按按照如期进行工作,值为null。
但是,如果activity是休眠的(在后台运行)然后用户点击通知,getIntent总是返回第一个intent,同时开启activity,但并不是通知的那个intent。
为了在app运行时,抓住通知中的intent,简单使用如下的代码。
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); and then override onNewIntent(Intent newintent). So when an application first runs, getIntent() can be used and when application is resumed from sleeping, onNewIntent works.
相关文章推荐
- Android单击通知栏后返回正在运行的程序并跳转到指定fragment页面
- Android学习 (二十) 在fragment里用Intent跳转到其他Activity
- 关于fragment跳转activity的回传值问题
- 同一个activity中不同fragment的跳转
- 重新跳转到Activity,其中的fragment显示空白
- activity跳转到fragment_百度知道
- Activity跳转Activity,Fragment切换下一个Fragment
- Activity数据更新,及时通知下面的Fragment
- Android从Fragment跳转到Activity
- Android activity跳转到另一个activity的指定fragment页面
- 实现Fragment中组件到Activity中的跳转
- Fragment 跳转到Activity onActivityResult方法不响应
- android从activity跳转到fragment解决
- 安卓fragment跳转activity,fragment跳转fragment,activity跳转fragment
- 兔子--activity跳转到fragment
- 从activity(A)跳转到其他activity(B)下面的fragment
- Fragment中的Activity跳转到MainActivity中的其他Fragment方法
- BaseFragment onClick中点击事件跳转到Activity
- 为Fragment内的按钮添加监听并跳转到另一个Activity
- Activity 跳转 fragment