簡體   English   中英

使用警報管理器在Android中面臨通知問題

[英]Facing notification problems in android using Alarm manager

因此,我試圖每天早上10點左右將通知發送給用戶。 用戶可以選擇取消每日通知。

我遇到了一些問題。 我在上午10點發出通知。 我收到了 但是,它會在幾秒鍾或幾分鍾后引發另一個通知。 我正在嘗試在單獨的可運行線程中設置此通知。 我能夠成功實現cancel選項,但是我遇到了這個多通知問題。 在這里,我附上代碼的一部分:

class Signup implements Runnable {

    @Override
    public void run() {
        Calendar updateTime = Calendar.getInstance();
        updateTime.set(Calendar.HOUR_OF_DAY, 10);
        updateTime.set(Calendar.MINUTE, 03);
        updateTime.set(Calendar.SECOND,10);

        Intent notification = new Intent(MainActivity.this, Alert.class);
        PendingIntent recurringNotification = PendingIntent.getBroadcast(MainActivity.this,
                0, notification, PendingIntent.FLAG_CANCEL_CURRENT);



        AlarmManager alarms = (AlarmManager) MainActivity.this.getSystemService(
                Context.ALARM_SERVICE);
        alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, recurringNotification);
    }
}

接收方:

public class Alert extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        dailyNotif(context,"title","Your Notif is here. Click to view.", 
             " here.");

    }
    public void dailyNotif(Context context, String title, String body, String alert){

        Intent notifyIntent = new Intent(context,AnotherNotif.class);
        PendingIntent notification = PendingIntent.getActivity(context,0,notifyIntent,0);

        NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
                                    .setSmallIcon(R.mipmap.ic_launcher)
                                    .setContentTitle(title)
                                    .setTicker(alert)
                                    .setContentText(body);

        builder.setContentIntent(notification);
        builder.setDefaults(NotificationCompat.DEFAULT_SOUND);
        builder.setAutoCancel(true);
        context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, builder.build());

    }


}

弄清楚為什么它會引發隨機通知。

如果時間已經過去,則在您運行應用程序時,它將立即發出通知。 因此,在這種情況下,如果

當前時間>鬧鍾時間

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM