0

當我點擊時,片段不會打開。我究竟做錯了什麼?這是我的gcm代碼。我在日誌聊天中看不到任何錯誤。通知起作用,我認爲沒有任何問題。Android gcm通知打開片段

任何人都可以幫助我解決這個問題嗎?

protected void onHandleIntent(Intent intent) { 


     extras=intent.getExtras(); 

     GoogleCloudMessaging gcm= GoogleCloudMessaging.getInstance(this); 


     String messageType=gcm.getMessageType(intent); 

     mes=extras.getString("title"); 
     //showToast(); 
     Log.e("Gcm Reciever : ", "(" + messageType + ")" + extras.getString("title")+" /// "); 
     //Log.e("Messaj : ",mes); 
     GcmBroadcastReceiver.completeWakefulIntent(intent); 
     showToast(); 
    } 
    public void showToast(){ 

     PendingIntent pIntent=PendingIntent.getActivity(this,0,new Intent(),0); 

     Notification noti=new Notification.Builder(this) 
       .setContentText(extras.getString("message")) 
       .setContentTitle(extras.getString("title")) 
       .setContentIntent(pIntent) 
       .setSmallIcon(R.drawable.mylogoticker) 
       .setTicker("Evren Time") 
       .getNotification(); 

     noti.flags=Notification.FLAG_AUTO_CANCEL; 

     noti.defaults |= Notification.DEFAULT_VIBRATE; 
     noti.sound = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.bildirim); 
     noti.ledARGB = 0xff00ff00; 
     noti.ledOnMS = 300; 
     noti.ledOffMS = 1000; 
     noti.flags |= Notification.FLAG_SHOW_LIGHTS; 

     noti.contentIntent=PendingIntent.getActivity(this,0,new Intent(this,MyFragment.class), 
       PendingIntent.FLAG_UPDATE_CURRENT); 

     NotificationManager manage=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); 

     manage.notify(0, noti); 

    } 

回答

1

在pIntent你留下了「新意圖()」空

PendingIntent pIntent=PendingIntent.getActivity(this,0,new Intent(),0); 

另外,不要叫片段直接,你應該改爲調用託管活動。這是因爲片段本身不存在,必須附加到活動。

檢查這個問題How to open fragment page, when pressed a notification in android

請注意,這是非常可取從GCM遷移到這裏FCM是一個指南,幫助您https://developers.google.com/cloud-messaging/android/android-migrate-fcm