Reputation: 445
I am trying to display a notification to the app users every day at 12pm and followed this example for the same. Nothing happens when the time reaches the target time. Why is this happening?
Here's my NotifyService
class:
public class NotifyService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate(){
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(), MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1, 0);
Notification mNotify = new Notification.Builder(this)
.setContentTitle("Log Tasks!")
.setContentText("Complete your tasks for today")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.setSound(sound)
.build();
mNM.notify(1, mNotify);
}
}
And here's how I call it in my MainActivity
class :
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView english_button;
private TextView hindi_button;
private PendingIntent pendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
english_button = findViewById(R.id.english_button);
hindi_button = findViewById(R.id.hindi_button);
english_button.setOnClickListener(this);
hindi_button.setOnClickListener(this);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
int i = preferences.getInt("numberoflaunches", 1);
if (i < 1){
alarmMethod();
i++;
editor.putInt("numberoflaunches", i);
editor.commit();
}
}
@Override
public void onClick(View v) {
Intent i = null;
switch (v.getId()){
case R.id.english_button:
i = new Intent(MainActivity.this, Instructions.class);
startActivity(i);
break;
case R.id.hindi_button:
i = new Intent(MainActivity.this, Instructions.class);
startActivity(i);
break;
}
}
private void alarmMethod(){
Intent myIntent = new Intent(this , NotifyService.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.AM_PM, Calendar.PM);
calendar.add(Calendar.DAY_OF_MONTH, 1);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 60 * 24, pendingIntent);
Toast.makeText(MainActivity.this, "Start Alarm", Toast.LENGTH_LONG).show();
}
}
I tried to narrow down the problem by logging and checking if alarmMethod()
was being called or not, but I saw that it was being called so I couldn't figure out what must be causing the problem here.
Here's the log :
2021-06-25 12:00:00.010 867-867/com.android.systemui D/KeyguardClockSwitch: Updating clock: 1200
2021-06-25 12:00:02.438 237-241/? E/[email protected]: Error opening kernel wakelock stats for: wakeup34: Permission denied
2021-06-25 12:00:02.435 237-237/? W/Binder:237_2: type=1400 audit(0.0:244): avc: denied { read } for name="wakeup34" dev="sysfs" ino=19014 scontext=u:r:system_suspend:s0 tcontext=u:object_r:sysfs:s0 tclass=dir permissive=0
2021-06-25 12:00:02.443 237-241/? E/[email protected]: Error opening kernel wakelock stats for: wakeup35: Permission denied
2021-06-25 12:00:02.439 237-237/? W/Binder:237_2: type=1400 audit(0.0:245): avc: denied { read } for name="wakeup35" dev="sysfs" ino=19077 scontext=u:r:system_suspend:s0 tcontext=u:object_r:sysfs:s0 tclass=dir permissive=0
2021-06-25 12:00:54.137 505-505/? E/netmgr: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:network' service: Invalid argument
2021-06-25 12:00:54.138 505-505/? E/netmgr: Failed to open QEMU pipe 'qemud:network': Invalid argument
2021-06-25 12:00:54.806 516-516/? E/wifi_forwarder: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:wififorward' service: Invalid argument
2021-06-25 12:00:54.807 516-516/? E/wifi_forwarder: RemoteConnection failed to initialize: RemoteConnection failed to open pipe
2021-06-25 12:01:00.008 867-867/com.android.systemui D/KeyguardClockSwitch: Updating clock: 1201
2021-06-25 12:01:00.332 576-2933/system_process W/ActivityManager: Background start not allowed: service Intent { cmp=com.google.android.apps.messaging/.shared.datamodel.action.execution.ActionExecutorImpl$EmptyService } to com.google.android.apps.messaging/.shared.datamodel.action.execution.ActionExecutorImpl$EmptyService from pid=10958 uid=10115 pkg=com.google.android.apps.messaging startFg?=false
2021-06-25 12:01:00.335 10958-10958/com.google.android.apps.messaging W/BugleDataModel: ActionExecutorImpl: Action started execution, but we can't guarantee it will complete, the app may be killed. Action: class com.google.android.apps.messaging.shared.datamodel.action.CountryCodeDetectorAction-CountryCodeDetectorAction:3271432003
java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.google.android.apps.messaging/.shared.datamodel.action.execution.ActionExecutorImpl$EmptyService }: app is in background uid UidRecord{439a600 u0a115 CEM idle procs:1 seq(0,0,0)}
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1715)
at android.app.ContextImpl.startService(ContextImpl.java:1670)
at android.content.ContextWrapper.startService(ContextWrapper.java:720)
at com.google.android.apps.messaging.shared.datamodel.action.execution.ActionExecutorImpl.a(PG:98)
at com.google.android.apps.messaging.shared.datamodel.action.execution.ActionExecutorImpl.a(PG:93)
at gjl.c(PG:119)
at gji.run(Unknown Source:1)
at afou.run(PG:3)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2021-06-25 12:01:00.344 10958-10958/com.google.android.apps.messaging W/BugleDataModel: ActionExecutorImpl: Action started execution, but we can't guarantee it will complete, the app may be killed. Action: class com.google.android.apps.messaging.shared.datamodel.action.SelfParticipantsRefreshAction-SelfParticipantsRefreshAction:3271432004
java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.google.android.apps.messaging/.shared.datamodel.action.execution.ActionExecutorImpl$EmptyService }: app is in background uid UidRecord{439a600 u0a115 CEM idle procs:1 seq(0,0,0)}
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1715)
at android.app.ContextImpl.startService(ContextImpl.java:1670)
at android.content.ContextWrapper.startService(ContextWrapper.java:720)
at com.google.android.apps.messaging.shared.datamodel.action.execution.ActionExecutorImpl.a(PG:98)
at com.google.android.apps.messaging.shared.datamodel.action.execution.ActionExecutorImpl.a(PG:93)
at gjl.c(PG:119)
at gji.run(Unknown Source:1)
at afou.run(PG:3)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Also, I have added the relevant user permissions line in the manifest file as follows but I am still getting no notification:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
Upvotes: 1
Views: 977
Reputation: 73
Maybe you have not registered the notification channel because starting from Android 8.0 (SDK 26), you should register a notification channel, otherwise sending notification will never work.
Look at it here: https://developer.android.com/training/notify-user/channels
Caution: If you target Android 8.0 (API level 26) and post a notification without specifying a notification channel, the notification does not appear and the system logs an error.
Creating a notification channel is easy, just create before calling NotificationManager.notify()
. You can refer to the example below that webpage (https://developer.android.com/training/notify-user/channels).
This is for Kotlin
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Create the NotificationChannel
val name = getString(R.string.channel_name)
val descriptionText = getString(R.string.channel_description)
val importance = NotificationManager.IMPORTANCE_DEFAULT
val mChannel = NotificationChannel(CHANNEL_ID, name, importance)
mChannel.description = descriptionText
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(mChannel)
}
This is for Java
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
Upvotes: 1