在特定时间向用户发送通知 Android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22836669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:29:17  来源:igfitidea点击:

Notification sent to user at a specific time Android

androidnotificationsalarmmanager

提问by Connor McFadden

I'm trying to send a notification to a user at a specific time using an Alarm Manager. Basically, nothing happens at all and to me the code looks fine. My code for the Alarm Manager is below:

我正在尝试使用警报管理器在特定时间向用户发送通知。基本上,什么都没有发生,对我来说代码看起来很好。我的警报管理器代码如下:

/** Notify the user when they have a task */
public void notifyAtTime() {
    Intent myIntent = new Intent(PlanActivity.this , Notification.class);     
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    PendingIntent pendingIntent = PendingIntent.getService(PlanActivity.this, 0, myIntent, 0);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 12);
    calendar.set(Calendar.MINUTE, 17);
    calendar.set(Calendar.SECOND, 00);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pendingIntent);
}

The code for the notification, which is in the "Notification" class is below:

“通知”类中的通知代码如下:

    public class Notification extends Service {


    @Override
    public void onCreate() {   

        Toast.makeText(this, "Notification", Toast.LENGTH_LONG).show();

        Intent intent = new Intent(this,PlanActivity.class);
        PendingIntent pending = PendingIntent.getActivity(this, 0, intent, 0);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Football")
                .setContentText("Don't forget that you have Football planned!")
                .setContentIntent(pending);

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(0, mBuilder.build());
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}

The Toast which is set in the Notification class also doesn't come up. I don't know if it's something really silly that I'm missing but any help would be greatly appreciated! :)

在 Notification 类中设置的 Toast 也没有出现。我不知道我错过的东西是否真的很愚蠢,但任何帮助将不胜感激!:)

回答by Saj

This is your receiver class :

这是您的接收器类:

 public class OnetimeAlarmReceiver extends BroadcastReceiver {

  public void onReceive(Context context, Intent intent) {

  //Toast.makeText(context, "Repeating Alarm worked.", Toast.LENGTH_LONG).show();


  // try here

 // prepare intent which is triggered if the
 // notification is selected

  Intent intent = new Intent(this, NotificationReceiver.class);
  PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

 // build notification
 // the addAction re-use the same intent to keep the example short
 Notification n  = new Notification.Builder(this)
    .setContentTitle("New mail from " + "[email protected]")
    .setContentText("Subject")
    .setSmallIcon(R.drawable.icon)
    .setContentIntent(pIntent)
    .setAutoCancel(true)
    .addAction(R.drawable.icon, "Call", pIntent)
    .addAction(R.drawable.icon, "More", pIntent)
    .addAction(R.drawable.icon, "And more", pIntent).build();


    NotificationManager notificationManager = 
     (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    notificationManager.notify(0, n); 


   }
 }

In Second Activity to set Alerm :

在第二个活动中设置警报:

  private void setAlarm(){

    Intent intent = new Intent(this, OnetimeAlarmReceiver.class);   

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, REQUEST_CODE, intent, 0);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + alarm_time , pendingIntent);
    System.out.println("Time Total ----- "+(System.currentTimeMillis()+total_mili));


} 

I would like to recommend this tutorial How to send notification

我想推荐这个教程如何发送通知

回答by Amine ZGHIDI

Try to put your notification code in the onStartCommand function instead of onCreate.

尝试将您的通知代码放在 onStartCommand 函数而不是 onCreate 中。

回答by Moses Kirathe

Writing the alarmmanager code in the onCreate method of the LAUNCHER activity of the app solved my problem

在应用的LAUNCHER活动的onCreate方法中编写alarmmanager代码解决了我的问题