2014-12-02 3 views
0

что не так с моим кодом ниже? Я ожидаю, что MainActivity будет открыта после того, как уведомление будет затронуто/нажато. мой код ниже:нажмите на уведомление, не открывающее активность

private void sendNotification(Quote quote) { 
    mNotificationManager = (NotificationManager) 
      this.getSystemService(Context.NOTIFICATION_SERVICE); 

    String message = quote.getQuote() + " - " + quote.getAuthor(); 

    // Creates an Intent for the Activity  
    Intent notifyIntent = new Intent(this, MainActivity.class); 
    notifyIntent.putExtra(Intent.EXTRA_SUBJECT, DailyQuotes.NOTIFICATION_QOD_MODE); 
    notifyIntent.putExtra(Intent.EXTRA_TEXT, quote.getQuoteID()); 
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 


    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
       notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
    .setSmallIcon(R.drawable.ic_launcher) 
    .setContentTitle(getString(R.string.qod_title)) 
    .setStyle(new NotificationCompat.BigTextStyle() 
    .bigText(message)) 
    .setContentText(message); 

    mBuilder.setContentIntent(contentIntent); 
    mNotificationManager.notify(DailyQuotes.NOTIFICATION_QOD_ID, mBuilder.build()); 
} 

Пожалуйста, помогите мне в этом.

+0

попробовать это 'PendingIntent contentIntent = PendingIntent.getActivity (это, 0, notifyIntent, 0);' –

ответ

0

попробовать это:

mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
mBuilder = new NotificationCompat.Builder(this); 
int requestID = (int) System.currentTimeMillis(); 
Intent notificationIntent = new Intent(this, 
     MainActivity.class); 
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
     | Intent.FLAG_ACTIVITY_CLEAR_TOP); 
PendingIntent contentIntent = PendingIntent.getActivity(
     this, requestID, notificationIntent, 
     PendingIntent.FLAG_UPDATE_CURRENT); 
mBuilder.setContentText("This is test"); 
mBuilder.setContentIntent(contentIntent); 
mBuilder.setContentTitle("Title").setSmallIcon(
     R.drawable.ic_launcher); 
mNotifyManager.notify(requestID, mBuilder.build()); 

вы можете изменить флаги.

надеюсь, что это поможет. Я изменил идентификатор запроса.

+0

я просто попытался и это не работает. все такой же. я думаю, что между моим кодом и твоей записью не так много. – yodann

+0

Я думаю, что вы на самом деле правы. но мне нужно добавить еще больше в файл манифеста. Я добавил android: launchMode = "singleTop" для MainActivity. Я не знаю почему, но кажется, что это работает позже. – yodann

0

вы должны использовать уникальный идентификатор для каждого уведомления. смотрите здесь. How to create Multiple statusbar Notifications in android

+0

Я не думаю, что это уникальный идентификатор. я не смог получить правильное действие. – yodann

Смежные вопросы