通知バーからActivityにジャンプし、Fragmentにジャンプ

11215 ワード

オーロラプッシュを使用します.プッシュを受け取り、通知を出す.部分コード
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()メソッドはacitivityをロードするときに最初のintentを返すからです.
したがって、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.