2017-07-25 194 views
1

我想在我按fcm通知時調用onNewIntent()方法。android通知pendingintent onNewintent不叫

我讀了一些問題Q &關於這一點,我想我讓我的代碼簡單。

但它不起作用。我不知道什麼是錯:(

這裏是我的代碼

FCM:

private void sendPushNotification(String message) { 

    System.out.println("received message :" + message); 
    Intent intent = new Intent(this, MainActivity.class); 
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(intent); 

    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.untitled) 
      .setContentTitle("This is title") 
      .setContentText(message) 
      .setPriority(Notification.PRIORITY_MAX) 
      .setVibrate(new long[]{0,500}) 
      .setContentIntent(pendingIntent); 


    PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE); 
    PowerManager.WakeLock wakelock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG"); 
    wakelock.acquire(5000); 

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(0, notificationBuilder.build()); 
} 

onNewIntent():

@Override 
public void onNewIntent(Intent intent){ 
    super.onNewIntent(intent); 
    setIntent(intent); 
    Bundle extras = intent.getExtras(); 
    if(extras != null){ 

      Toast.makeText(getApplicationContext(),"Hello!",Toast.LENGTH_LONG).show(); 

    } 
} 

和清單:

<activity android:name=".MainActivity" 
     android:launchMode="singleTop"> 

而且只有兩個錯誤我n logcat:

07-25 18:38:18.216 25648-25648 /? E/Zygote:v2

07-25 18:38:18.221 25648-25648 /? E /合子:accessInfo:0

+0

試試.... Intent intent = new Intent(this,MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); –

+0

感謝您的評論!但這對我不起作用T.T –

+0

張貼您的logcat錯誤的屏幕截圖,請致電 – Nenco

回答

0

重新發佈一個解決方案,我發現在這裏: click on notification to go current activity

添加這些行:

intent.setAction(Intent.ACTION_MAIN); 
intent.addCategory(Intent.CATEGORY_LAUNCHER); 

我不知道他們爲什麼必須設置的,但如果你不」將它們設置爲NewIntent將不會被調用。

相關問題