How schedule local notifications Android?

给你一囗甜甜゛ 提交于 2019-11-27 18:31:36

问题


i have a question about local notifications in Android. I am developing an application where in first part must receive all meetings of company of my own server (This i have achieved), and the second part i must notify one day before each meeting, but with local notifications.

How schedule local notifications at given date?


回答1:


To schedule local notification you need to know some of the things which are used to schedule the notification like

BroadcastReceivers IntentFilters AlarmManager NotificationService PendingIntent

In MainActivity do the following

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
        notificationIntent.addCategory("android.intent.category.DEFAULT");

        PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, 15);
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);
    }

The above code will schedule an alarm after 15 seconds. After 15 seconds it will broadcast the notificationIntent.

The action specified in the Intent constructor is defined in the AndroidManifest.xml

To understand the full working of local notification and to see a sample notification code check out this article - http://www.singhajit.com/schedule-local-notification-in-android/



来源:https://stackoverflow.com/questions/23106006/how-schedule-local-notifications-android

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