Вопрос

I have an app which is counting down the time. I would like to get a notification in the notification bar while the time is up.

I've already done this:

Intent intent = new Intent();
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification noti = new Notification.Builder(this).setTicker("Ticker Title").setContentTitle("Content Title").setContentText("Notification content.").setSmallIcon(R.drawable.iconnotif).setContentIntent(pIntent).getNotification();
noti.flags=Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(uniqueID, noti);

The problem occurs when I try to enter the app by clicking the notification. When the notification is shown, I click it, but it doesn't do anything.

How to solve this problem? Thanks for help! :)

Это было полезно?

Решение

Your Notification has a PendingIntent. This is used to define behaviour for clicking the notification. A pending intent has an intent as well, this intent contains info about application to launch, or in general, what to do after clicking the notification.

In your example, however, the intent that is included in your Pending Intent is:

// Empty intent, not doing anything
Intent intent = new Intent();

E.g. your intent does not define what to do. Change your intent to something like this:

// New Intent with ACTION_VIEW: 
Intent intent = new Intent(Intent.ACTION_VIEW);

// Activity to launch
intent.setClassName("your.package.name", "ActivityToLaunch");

// Intent Flags
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top