Reputation: 16219
I know might be duplicate but I'm using AlarmManager to get notification after few interval
I used following code to show notification
public class MainActivity extends Activity {
public void setRepeatingAlarm() {
Intent intent = new Intent(this, TimeAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
(50 * 1000), pendingIntent);
}
}
public class TimeAlarm extends BroadcastReceiver {
NotificationManager nm;
@Override
public void onReceive(Context context, Intent intent) {
nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(), 0);
Notification noti = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("ticker")
.setWhen(System.currentTimeMillis()+50000)
.setContentTitle("title")
.setContentText("Check out ")
.setContentIntent(contentIntent)
//At most three action buttons can be added
.setAutoCancel(true).build();
int notifyID =1;
nm.notify(notifyID, noti);
}
}
All is good only but on click of that notification icon not opening my APP`
Upvotes: 0
Views: 5430
Reputation: 16219
Finally I made it it working now thanks a lot all
@Override
public void onReceive(Context context, Intent intent) {
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification noti = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("ticker")
//.setLargeIcon(largeIcon)
//.setWhen(System.currentTimeMillis()+1000)
.setContentTitle("title")
.setContentText("message!")
.setContentIntent(contentIntent)
//At most three action buttons can be added
.setAutoCancel(true).build();
int notifyID =0;
notificationManager.notify(notifyID, noti);
}
Upvotes: 0
Reputation: 685
Take a look at the code I have used, it works perfect for me.
Long rowId = intent.getExtras().getLong(TaskDatabase.KEY_ROWID);
String a = intent.getExtras().getString(TaskDatabase.KEY_TITLE);
int count = 0;
int i = 1;
String body = "Task needs your attention.";
String title = "Reminder";
NotificationManager mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, Main.class); // Change it any activity, you want it to open.
notificationIntent.putExtra(TaskDatabase.KEY_ROWID, rowId);
PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification note = new Notification(R.drawable.white, body, System.currentTimeMillis());
note.setLatestEventInfo(this, a, body, pi);
SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String ringPreference = preference.getString("Ringtone", "DEFAULT_SOUND");
note.defaults |= Notification.DEFAULT_SOUND;
note.flags |= Notification.FLAG_AUTO_CANCEL;
note.sound = Uri.parse(ringPreference);
int id = (int)((long)rowId);
mgr.notify(id, note);
Upvotes: 2