java - 需要一些帮助对安排每日通知的代码进行故障排除

标签 java android-studio android-intent push-notification android-notifications

我是 Android 编程新手,最近刚刚开始开发我的第一个应用程序。我正在尝试创建一个每日通知,用户每天都会在同一时间收到。我查看了文档和一些教程并提出了这个。由于某种原因,下面的代码不起作用。它没有错误,运行得很好,但没有完成工作,我似乎找不到问题。还有一些代码负责在设备重新启动时重新安排通知,但我认为问题不在那里,因为我什至没有收到初始通知。

MainActivity.java

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

    show = (Button)findViewById(R.id.btn_show);
    show.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startAlarm(true,true);
        }
    });

    myWebView = (WebView)findViewById(R.id.webView1);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.loadUrl("http://google.com");
    myWebView.setWebViewClient(new WebViewClient());
}

private void startAlarm(boolean isNotification, boolean isRepeat) {
    AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    Intent myIntent;
    PendingIntent pendingIntent;

    // SET TIME HERE
    Calendar calendar= Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY,14);
    calendar.set(Calendar.MINUTE,45);


    myIntent = new Intent(MainActivity.this,AlarmNotificationReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(this,0,myIntent,0);


    if(!isRepeat)
        manager.set(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime()+3000,pendingIntent);
    else
        manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,pendingIntent);
}

AlarmNotificationReciever.Java

public class AlarmNotificationReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

        Intent myIntent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(
                context,
                0,
                myIntent,
                FLAG_ONE_SHOT );

        builder.setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Zodiac")
                .setContentIntent(pendingIntent)
                .setContentText("Check out your horoscope")
                .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
                .setContentInfo("Info");

        NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1,builder.build());
    }
}

它基本上应该安排在按下按钮后的 14:45 发出通知,但由于某种原因它没有。

最佳答案

  1. 自 Android Oreo 起,隐式广播接收器在以下情况下将无法工作
    在AndroidManifest.xml中注册

  2. 要在应用程序中使用隐式接收器,您需要在代码中以编程方式定义它们,使用 注册接收器()。

3.使用registerReceiver(),我们可以在 Activity 的生命周期中以编程方式注册和注销Receiver()。这边走 仅当我们的 Activity/应用程序处于 Activity 状态,而在其他时间则不活动。

我们工作正常:

     [public class MainActivity extends AppCompatActivity {
            Button  show;
            WebView  myWebView;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

             // register custom intent filter

               *registerReceiver(new AlarmNotificationReceiver(),new IntentFilter(Intent.ACTION_BATTERY_CHANGED));*

\[enter image description here\]\[1\]
                show = (Button)findViewById(R.id.btn_show);
                show.setOnClickListener(new View.OnClickListener() {
                    @RequiresApi(api = Build.VERSION_CODES.N)
                    @Override
                    public void onClick(View v) {
                        startAlarm(true,true);
                    }
                });
                myWebView = (WebView)findViewById(R.id.webView1);
                WebSettings webSettings = myWebView.getSettings();
                webSettings.setJavaScriptEnabled(true);
                myWebView.loadUrl("http://google.com");
                myWebView.setWebViewClient(new WebViewClient());
            }
            @RequiresApi(api = Build.VERSION_CODES.N)
            private void startAlarm(boolean isNotification, boolean isRepeat) {
                AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
                Intent myIntent;
                PendingIntent pendingIntent;
                // SET TIME HERE
                Calendar calendar= Calendar.getInstance();
                calendar.set(Calendar.HOUR_OF_DAY,14);
                calendar.set(Calendar.MINUTE,45);
                myIntent = new Intent(MainActivity.this,AlarmNotificationReceiver.class);
                pendingIntent = PendingIntent.getBroadcast(this,0,myIntent,0);
                if(!isRepeat)
                    manager.set(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime()+3000,pendingIntent);
                else
                    manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,pendingIntent);
            }
        }][1]

关于java - 需要一些帮助对安排每日通知的代码进行故障排除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57592470/

相关文章:

java - 垂直系统拥塞

android-studio - 如何在 Kotlin 文件中的 Android Studio 中控制何时将 Imports 替换为通配符

带有 AndroidAnnotations 的 Android Studio -> 这些类型将不会进行注释处理

android - 为什么 PendingIntent 不发回我的 Intent 自定义 Extras 设置?

java - 文件时间到字符串

java - SugarCRM、WSDL 和 RPC

android - 如果我将 List<Child> 放入一个 Intent 中,那么我会从另一个 Activity 中检索 List<Parent>

Android:如何自定义来电屏幕

java - 通过 http 请求以字节形式发送 java 对象 : is BASE64 necessary?

安卓工作室 : How to debug a running process on Android device?