Reputation: 1173
I'm new to android programming and I just learned how to make a notification
via an Intent
and a PendingIntent
. I wondered if I could make this notification clickable (link to my MainActivity
) without starting a new Activity
.
This is what I have right now (AlarmReceiver
excluded) as a function in my MainActivity
:
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
manager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
I reckoned I should change "this" to something else, but I wouldn't know how to do this. Any help would be appreciated!
Upvotes: 0
Views: 815
Reputation: 162
Intent alarmIntent = new Intent(this, MainActivity.class);
PendingIntent intent = PendingIntent.getActivity(this, 0,
alarmIntent , PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
manager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); ...try this
Upvotes: 0
Reputation:
You can use the notification Builder, in my case the compat builder.
NotificationCompat.Builder
Setting the content, I've got mine from the params. You have to replace them with your respected once.
builder.setContentTitle(params.getTitle())
.setContentText(params.getText())
.setWhen(params.getTime())
.setTicker(params.getTickerText())
.setPriority(params.getPriority())
.setOngoing(params.isOngoing())
.setOnlyAlertOnce(true)
.setAutoCancel(params.getAutoCancel())
.setStyle(params.getStyle());
Creating PendingIntent and also adding it to the Notification through the builder. This intent should be pointing to your Launcher Activity and from there you can handle it in OnCreate.
builder.setContentIntent(pendingIntent);
Then push the notification
mNotificationManager.notify(id, notification);
You can also make these notifications pointing to a BroadCastReceiver and handle a click on the notification from there, on its onReceive method.
Upvotes: 2