Reputation: 274
Not sending notification at selected time, when I ran my code, directly showed notification and showed error as well Here is the error message: E/NotificationManager: notifyAsUser: tag=null, id=12345, user=UserHandle{0} I thought the error message was due to Build.VERSION.SDK_INT, but after adding that, the error message is still there.
Place all of these under onCreate:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 13);
calendar.set(Calendar.MINUTE,9)
;
Intent intent = new Intent ();
intent.setAction("com.example.Broadcast");
PendingIntent contentIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// PendingIntent alarmIntent = PendingIntent.getBroadcast(this,0, intent,0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, contentIntent);
and here is the extend.
public class wakeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
setNotification(context);
}
protected void setNotification(Context context){
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, MainActivity.class), 0);
String ChannelId = "12345";
int uniID = 12345;
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,ChannelId )
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle("Hi")
.setAutoCancel(true)
.setWhen(System.currentTimeMillis())
.setContentText("Please Rate.");
builder.setContentIntent(contentIntent);
// // Send notification to your device NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId("com.myApp");
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
"com.myApp",
"My App",
NotificationManager.IMPORTANCE_DEFAULT
);
if (manager != null) {
manager.createNotificationChannel(channel);
}
}
manager.notify(uniID, builder.build());
}
}
Can someone please help me with this?
Upvotes: 0
Views: 60
Reputation: 95578
You are very confused.
In your code you call NotificationManager.notify()
. This will show the Notification
immediately.
You do:
Intent intent = new Intent(this, MainActivity.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(this,0, intent,0);
This won't work. You have created a PendingIntent
which will be sent via broadcast using an Intent
that is for an Activity
! What do you want to happen? Do you want an Activity
to be launched or do you want a BroadcastReceiver
to be triggered?
I think what you want to do is as follows:
Intent
for a BroadcastReceiver
, wrap that in a PendingIntent
using getBroadcast()
and pass that to the AlarmManager
so that the broadcast Intent
will be set at some future time.extends BroadcastReceiver
. In onReceive()
create the Notification
and call NotificationManager.notify()
to post the Notification
. In the Notification
you can set a PendingIntent
that opens your Activity
so that if the user clicks on the Notification
your Activity
will be launched. To do this, call PendingIntent.getActivity()
and pass an Intent
that contains MainActivity.class
.Upvotes: 0