使用极光推送。
收到推送,发出通知。
部分代码
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.