Reputation: 17
Fairly new to java/android but getting better slowly. I am trying to create a one time notification to remind the user about the app 24 hours after first run. I researched online and using stackoverflow, I was finally able to get the code to run with the following notification code:
private void addNotification() {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Notifications Example")
.setContentText("This is a test notification");
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, builder.build());
}
I am trying to call the notification function but it is not working. I am calling it as follows:
addNotification();
How do I proceed from here? I have not dealt with the 24 hr issue, just trying to get the notification to come up. Then upon clicking it, the app should start again.
Upvotes: 0
Views: 2860
Reputation: 10100
Try this :
You need to pass two parameters to addNotification(Context context, String Message)
.
Like addNotification(this,"My Notification");
private void addNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
String appname = context.getResources().getString(R.string.app_name);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification;
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, myactivity.class), 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
notification = builder.setContentIntent(contentIntent)
.setSmallIcon(icon).setTicker(appname).setWhen(0)
.setAutoCancel(true).setContentTitle(appname)
.setContentText(message).build();
notificationManager.notify(0 , notification);
}
For reminder you can implement AlarmManager or Timer.
Check these examples :
Upvotes: 1