Repeat notification every day 12h

瘦欲@ 提交于 2021-02-08 05:38:37

问题


I want repeat my notification every day at 12h, but my code isn't working... I launch my Alarm Manager in MainActivity in OnCreate like this :

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ma);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 12);
    calendar.set(Calendar.MINUTE, 00);
    calendar.set(Calendar.SECOND, 0);
    Intent intent1 = new Intent(MainActivity.this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0,intent1, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

And My AlarmReceiver Class :

public class AlarmReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(context, MainActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);


    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
            context).setSmallIcon(R.drawable.photo)
            .setContentTitle("ça marche fdp")
            .setContentText("Et ouai t'as bien réussi à afficher une notification bordel de cul").setSound(alarmSound)
            .setAutoCancel(true).setWhen(when)
            .setContentIntent(pendingIntent)
            .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
    notificationManager.notify(0, mNotifyBuilder.build());



}

}

Do you know where is the problem ? Please don't dislike I just want learn code :(


回答1:


Update your manifest file to have the following parameters for your receiver:

<receiver
        android:name="com.example.alarmmanagernotifcation.AlarmRecei‌​ver"
        android:enabled="true"
        android:process=":remote" />

Then I believe the problem could be with the icon you have chosen with:

.setSmallIcon(R.drawable.photo)

If the image is not compatible then you will not see a crash outside the app, it will only show a fatal exception inside of the android monitor if the phone is plugged in, so it can go unnoticed and the notification will not be fired.

The drawable that is used needs to have the different sizes for different phone densities. To make the correct drawables, right click on your drawable package and do New -> Image Asset. From the pulldown select Notification Icons, and use that to generate all the different sizes for the icon.



来源:https://stackoverflow.com/questions/41643743/repeat-notification-every-day-12h

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!