启动时预定的Android Notification推送

时间:2016-04-11 13:00:14

标签: android android-notifications

我正在开发Android的日程安排通知。它在预定时间成功获得通知,但在我不想要的应用程序启动时也会收到通知。这是我的代码。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    prefs= PreferenceManager.getDefaultSharedPreferences(this);

    show = (TextView)findViewById(R.id.textView);

    View btn_failure = findViewById(R.id.btn_failure_id);
    btn_failure.setOnClickListener(this);
    View btn_unhappy = findViewById(R.id.btn_unhappy_id);
    btn_unhappy.setOnClickListener(this);
    View btn_chicken_soup = findViewById(R.id.btn_soup_id);
    btn_chicken_soup.setOnClickListener(this);
    View btn_no_motivation = findViewById(R.id.btn_no_id);
    btn_no_motivation.setOnClickListener(this);

//  createScheduledNotification(9,0);

}

@Override
protected void onResume() {
    super.onResume();

    time = prefs.getString("noti", "9:00");
    String[] pieces=time.split(":");
    hour = Integer.parseInt(pieces[0]);
    minute = Integer.parseInt(pieces[1]);
    boolean onOff = prefs.getBoolean("noti_10",true);
    createScheduleNotification(hour,minute,onOff);
}

public void createScheduleNotification(int hour, int minute, boolean onOff){
    if(onOff){
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, 0);
        AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);
        am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, getPendingIntent(this));
    }else{
        AlarmManager mgr=(AlarmManager)this.getSystemService(this.ALARM_SERVICE);
        mgr.cancel(getPendingIntent(this));
    }
}

private PendingIntent getPendingIntent(Context ctxt) {
    Intent intent1 = new Intent(ctxt, TimeAlarm.class);
    intent1.putStringArrayListExtra("quotes",quote_chicken_soup);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(ctxt, 0,intent1, PendingIntent.FLAG_UPDATE_CURRENT);
    return pendingIntent;
}


public class TimeAlarm extends BroadcastReceiver {

    private String daily_quote = "testing";

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

        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent intent = new Intent(context,DisplayActivity.class);
        intent.putStringArrayListExtra("quote",paramIntent.getStringArrayListExtra("quotes"));
        daily_quote = paramIntent.getStringArrayListExtra("quotes").get(0);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent, 0);

        Notification.Builder builder = new Notification.Builder(context);

        builder.setAutoCancel(true);
        builder.setTicker("Daily Quote");
        builder.setContentTitle("Daily Quote");
        builder.setContentText(daily_quote);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentIntent(pendingIntent);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            builder.setSubText("Be Happy! Have a nice day!");   //API level 16
        }

        Notification myNotication = builder.getNotification();
        manager.notify(11, myNotication);
    }

}

在应用启动时导致通知被推送的问题在哪里?

2 个答案:

答案 0 :(得分:1)

你的代码似乎没有错。 在启动时推送通知的问题肯定是因为AlarmManager代码。

在您的代码段中,我将时间设置硬编码如下,并按预期运行。第一次在系统时钟后一分钟触发,之后每隔2分钟触发一次。

      // first time set to-- trigger after one minute from now
long firstTimeTriggerAt = SystemClock.elapsedRealtime() + 1000 * 60 * 1;
        AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);
       //Repeat the alaram every two minutes
        long interval =  1000 * 60 * 2;
    am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTimeTriggerAt, interval, getPendingIntent(this));

因此,您需要检查您在共享首选项中设置的时间常量值。  仅供参考,代码也可以在RTC_WAKEUP常量下运行良好。

希望这有帮助。

答案 1 :(得分:0)

onResume()中的

更改

boolean onOff = prefs.getBoolean("noti_10",true);

boolean onOff = prefs.getBoolean("noti_10",false);