【问题标题】:Notification not showing when my alarm triggers onReceive() [duplicate]当我的警报触发 onReceive() 时未显示通知 [重复]
【发布时间】:2018-08-31 21:41:56
【问题描述】:

当我的警报管理器触发我的 onReceive() 方法时,我试图弹出一个通知。这就是我所做的

@Override
public void onReceive(Context context, Intent intent) {
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
    //Acquire the lock
    wl.acquire(10000);

    startNotification(context);

    wl.release();

}


public void setAlarm(Context context){
    AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
    intent.putExtra(Activity, "MainActivity.class");
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
    assert am != null;
    am.set(AlarmManager.RTC_WAKEUP, 60000, pi);
}


private void startNotification(Context context){
    // Sets an ID for the notification
    int mNotificationId = 001;
    NotificationManager notificationManager;
    NotificationCompat.Builder mBuilder;

            // Build Notification , setOngoing keeps the notification always in status bar
    mBuilder =
            new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.ic_launcher_foreground)
                    .setContentTitle("RandomTitle")
                    .setContentText("RandomText")
                    .setOngoing(true);

    // Create pending intent, mention the Activity which needs to be
    //triggered when user clicks on notification(StopScript.class in this case)

    Intent notificationIntent = new Intent(context, MainActivity.class);
    notificationIntent.putExtra("extra","Extra Notificacion");
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent , PendingIntent.FLAG_UPDATE_CURRENT);
   // context.startActivity(notificationIntent);

    mBuilder.setContentIntent(contentIntent);


    // Gets an instance of the NotificationManager service
     notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    //Android Oreo
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel("notify_001",
                "Channel human readable title",
                NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }

    // Builds the notification and issues it.
    notificationManager.notify(mNotificationId, mBuilder.build());
}

我真的很困惑为什么这个通知没有显示,我已经测试了我的警报,它在创建 1 分钟后触发,但通知仍然没有显示。

有什么想法吗?

谢谢

【问题讨论】:

  • 应用在哪个操作系统上运行?
  • 安卓牛轧糖 7.1
  • 你能告诉我你在gradle中的targetSdkVersion吗?
  • targetSdkVersion 28
  • 请看我的回答。

标签: java android notifications


【解决方案1】:

来自 Android 开发者:

当您面向 Android 8.0(API 级别 26)时,您必须实现一个或 更多通知渠道。如果您的 targetSdkVersion 设置为 25 或 更低,当您的应用在 Android 8.0(API 级别 26)或更高版本上运行时,它 行为与运行 Android 7.1 的设备相同(API 级别 25) 或更低。

因为你的targetSdkVersion是28,所以你也必须在Builder构造函数中添加channelId

将您的代码更改为:

// Build Notification , setOngoing keeps the notification always in status bar
mBuilder = new NotificationCompat.Builder(context, "notify_001") // Add channel ID to the constructor.
        .setSmallIcon(R.drawable.ic_launcher_foreground)
        .setContentTitle("RandomTitle")
        .setContentText("RandomText")
        .setOngoing(true);

【讨论】:

    【解决方案2】:

    根据 Android 开发者文档:

    NotificationCompat.Builder NotificationCompat.Builder(上下文上下文) 此构造函数在 API 级别 26.1.0 中已弃用。采用 NotificationCompat.Builder(Context, String) 代替。所有已发布 通知必须指定 NotificationChannel Id。

    参考here
    编辑: 请尝试以下操作:

    1. NotificationCompat.Builder 保持原样(使用字符串 代表NotificationChannel)。
    2. 注释掉创建通知通道的 if 块

    3. 将您的NotificationManager 替换为NotificationManagerCompat,如下所示:

      NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
      
                  notificationManager.notify(mNotificationId, mBuilder.build());
      

    【讨论】:

    • 刚刚改了还是不行
    • 您是否添加了与您正在创建的频道相同的名称?
    • 是的,但仍然无法正常工作
    • 我更新了我的评论
    • 还是一样,不显示:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-15
    • 2020-04-01
    • 1970-01-01
    相关资源
    最近更新 更多