簡體   English   中英

android通知未發送

[英]android notification not being sent

我正在嘗試在用戶在datePicker上設置的日期向用戶發送通知。

沒有發送通知,我也不知道為什么。

變量contextonReceive定義為傳遞的上下文。

public void setAlarm() {
        int year = datePicker.getYear();
        int month = datePicker.getMonth();
        int day = datePicker.getDayOfMonth();

        Calendar c = Calendar.getInstance();
        c.set(year, month, day);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);

        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        Intent intent = new Intent(context, NotifyReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

        am.set(AlarmManager.RTC, c.getTimeInMillis(), pendingIntent);

        Toast.makeText(context, "Set reminder for: " + (month+1) + "/" + day + "/" + year, Toast.LENGTH_LONG).show();

    }

廣播接收器:

public class NotifyReceiver extends BroadcastReceiver {

    private static final int NOTIFICATION = 12368327;

    private NotificationManager mNM;

    private Context context;

    @Override
    public void onReceive(Context ctx, Intent intent) {
        mNM = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
        context = ctx;
        showNotification();
    }

    private void showNotification() {

        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);

        Notification noti = new Notification.Builder(context)
                .setContentTitle("Title")
                .setContentText("Text").setSmallIcon(R.drawable.ic_launcher)
                .setContentIntent(contentIntent).build();

        // Clear the notification when it is pressed
        noti.flags |= Notification.FLAG_AUTO_CANCEL;

        // Send the notification to the system.
        mNM.notify(NOTIFICATION, noti);
    }
}

當調用該方法意味着要設置警報時,我也會吐司。

試試這條線

請設置您的AlarmManager

am .setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 1000, 1000,pendingIntent_for_every_second);

謝謝

您應該通過減少一個來傳遞“ month”參數。

例如,如果您的月份是十月,那么您應該通過9而不是10

采用

c.set(year, month-1, day);

代替

c.set(year, month, day);

無需使用設置重復。

暫無
暫無

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

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