Как я могу открыть активность при нажатии уведомления

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

Мой код:

 private void sendNotification(String msg) {

        NotificationCompat.Builder mBuilder =  new NotificationCompat.Builder(this)
        .setContentTitle("EXX")
        .setSmallIcon(R.drawable.ic_launcher)
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(msg))
        .setContentText(msg)          
        .setOngoing(true);          
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}    

Это возможно,

Благодарю.


person John Lesh    schedule 03.02.2017    source источник
comment
Возможный дубликат Открыть приложение после нажатия на уведомление   -  person Murat Karagöz    schedule 03.02.2017
comment
Не дубликат. ОП просто не хватает setContentIntent()   -  person David Wasser    schedule 03.02.2017


Ответы (2)


Да, это возможно.

Измените свой метод, вот так;

private void sendNotification(String msg) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("yourpackage.notifyId", NOTIFICATION_ID);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder mBuilder =  new NotificationCompat.Builder(this)
            .setContentTitle("EXX")
            .setSmallIcon(R.drawable.ic_launcher)
            .setStyle(new NotificationCompat.BigTextStyle()
            .bigText(msg))
            .addAction(getNotificationIcon(), "Action Button", pIntent)
            .setContentIntent(pIntent)
            .setContentText(msg)
            .setOngoing(true);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

и добавьте свою основную активность

    NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

этот код работает.

person Berat Eyüboğlu    schedule 03.02.2017
comment
ой ну спасибо ! этот код работает на мне! я люблю тебя брат ‹3 - person John Lesh; 03.02.2017
comment
Имейте в виду, что addAction(getNotificationIcon(), "Action Button", pIntent) устарел - person Moussa; 31.08.2018

Эй, @Джон, это очень просто

ты просто должен сделать

 Intent intent = new Intent(this, ResultActivity.class);

... // Поскольку щелчок по уведомлению открывает новую («специальную») активность, // нет необходимости создавать искусственный задний стек.

PendingIntent pendingIntent = TaskStackBuilder.create(getApp())
            .addNextIntent(intent)
            .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

И установите отложенное намерение в mBuilder

private void sendNotification(String msg) {
        NotificationCompat.Builder mBuilder =  new NotificationCompat.Builder(this)
        .setContentTitle("EXX")
        .setSmallIcon(R.drawable.ic_launcher)
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(msg))
        .setContentText(msg)          
        .setOngoing(true);    
        .setContentIntent(pendingIntent)
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
} 

и вы сделали :)

person Umesh Sonawane    schedule 03.02.2017
comment
Вместо getApp() можно использовать MainActivity.class или имя конкретного действия, чтобы указать имя вашего действия. - person greg; 05.08.2020