【问题标题】:AlarmManager + NotificationManager not workingAlarmManager + NotificationManager 不工作
【发布时间】:2020-06-19 15:04:23
【问题描述】:

首先,我要感谢您点击此主题。 非常感谢您花时间阅读本文。

我正计划让应用程序在特定时间推送通知。 我在网上看了一些教程,但似乎不起作用。

代码本身没有错误消息,但没有发出通知。

这是我的“Notification_receiver.java”文件:

package com.example.reviewerapplication;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

public class Notification_receiver extends BroadcastReceiver {



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

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

    Intent repeating_intent = new Intent(context,Chapterchoice.class);
    String daily10 = "daily";
    intent.putExtra("daily",daily10);
    repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(context,100,repeating_intent,PendingIntent.FLAG_UPDATE_CURRENT);



    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setContentIntent(pendingIntent)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Sample Title")
            .setContentText("same message")
            .setAutoCancel(true);

    if (intent.getAction().equals("MY_NOTIFICATION_MESSAGE")) {
        notificationManager.notify(100, builder.build());
    }


  }
}

这是我的 MainActivity 上的内容:

       //Daily Notification
    val calendar = Calendar.getInstance()

    calendar.set(Calendar.HOUR_OF_DAY,21)
    calendar.set(Calendar.MINUTE,53)
    calendar.set(Calendar.SECOND,0)

    val intent2 = Intent(this, Notification_receiver::class.java)
    intent2.setAction("MY_NOTIFICATION_MESSAGE")
    var pendingIntent = PendingIntent.getBroadcast(this,100,intent2,PendingIntent.FLAG_UPDATE_CURRENT )
    var alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.timeInMillis,AlarmManager.INTERVAL_DAY,pendingIntent)

【问题讨论】:

  • 你在 manifest 中注册了 BroadcastReceiver 吗?
  • 感谢您的宝贵时间!我确实放了 对吗?或者我需要创建类似 我尝试添加它但它给了我一个错误 Unresolved class ".BroadcastReceiver"
  • 我认为它与Android Oreo 有什么关系?

标签: android notifications calendar alarmmanager notificationmanager


【解决方案1】:

其实我刚才已经修好了。

我的问题是我在清单中使用了“活动”而不是使用“接收者”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-25
    • 2019-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多