java - 提醒通知不显示

标签 java android broadcastreceiver alarmmanager

我正在制作一个应用程序来提醒用户一些事情。我想在将来的某个时间显示通知。我已经按照一些教程编写了下面的代码,但它似乎不起作用。在我期待收到通知时,它并没有出现。

我正在使用 BroadcastReceiverAlarmManager 在所需时间发出通知。这是我的(简化的)代码。

设置时间的代码:

try {
        Date date = format.parse(timeInput);//This part works
        long time = date.getTime();//Get the time in milliseconds

        Intent i = new Intent(getBaseContext(), AlarmReceiver.class);

        PendingIntent alarmSender = PendingIntent.getBroadcast(getBaseContext(), 0, i, 0);

        AlarmManager am = (AlarmManager) getBaseContext().getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, time, alarmSender);

        Toast.makeText(getBaseContext(), "Keep the app running to receive a reminder notification", Toast.LENGTH_LONG).show();

        super.onBackPressed();

    }catch(Exception e){
        Toast.makeText(getBaseContext(), "Parsing error. Format:\ndd/MM/yyyy and HH:mm", Toast.LENGTH_SHORT).show();
    }

AlarmReceiver.onReceive() 方法:

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

    Intent i = new Intent(context, MenuActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i, 0);


    NotificationCompat.Builder nBulder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.notify_icon)
            .setContentTitle("title")
            .setContentText("text")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true);
    NotificationManagerCompat nManager = NotificationManagerCompat.from(context);
    nManager.notify(0, nBulder.build());

}

一切都在 list 文件中正确声明。

最佳答案

   <receiver
        android:name=".AlarmReceiver"
        android:enabled="true"
        android:exported="true"></receiver>

    public class AlarmReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO: This method is called when the BroadcastReceiver is receiving
            // an Intent broadcast.
           throw new UnsupportedOperationException("Not yet implemented");
       }
   }

小改动:

    try {
        long time = System.currentTimeMillis();
        Intent i = new Intent(getApplicationContext(), AlarmReceiver.class);
        PendingIntent alarmSender = PendingIntent.getBroadcast(getApplicationContext(), 0, i, 0);
        AlarmManager am = (AlarmManager) getApplication().getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, time, alarmSender);
    } catch(Exception e){
        Toast.makeText(getBaseContext(), "Parsing error. Format:\ndd/MM/yyyy and HH:mm", Toast.LENGTH_SHORT).show();
    }

Difference between getContext() , getApplicationContext() , getBaseContext() and "this"

关于java - 提醒通知不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50198910/

相关文章:

android - 如何从 BroadcastReceiver 发送短信并检查其状态?

android - AlarmReceiver 没有被 android :process =":remote" 调用

java - log4j2 - Syslog appender 和 PatternLayout

java - 读取文本文件以选择脚本和进程

Android 自定义 XML 解析器无法解析 android 命名空间

安卓工作室 : how to setup generated Cloud Endpoints sources?

android - 如何更正 'Instrumentation run failed due to ' java.lang.IllegalArgumentException''

java - JPA:将上次更新时间存储在单独的表中

java - Eclipse 将 XText 编辑器添加到透视图

Android:在 Webview 中加载 facebook 页面