首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >当我的闹钟触发onReceive()时不显示通知

当我的闹钟触发onReceive()时不显示通知
EN

Stack Overflow用户
提问于 2018-09-01 05:41:57
回答 2查看 183关注 0票数 0

当我的警报管理器触发我的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分钟的beign创建,但通知仍然没有显示。

有什么想法吗?

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-01 06:47:49

来自Android developer:

当您以Android8.0(API26级)为目标时,您必须实现一个或多个通知通道。如果你的targetSdkVersion设置为25或更低,当你的应用程序在Android8.0(API26级)或更高版本上运行时,它的行为与在运行Android7.1(API25级)或更低版本的设备上相同。

因为您的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);
票数 1
EN

Stack Overflow用户

发布于 2018-09-01 06:21:45

根据Android开发人员文档:

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

参考here

编辑:尝试以下操作:

  1. NotificationCompat.Builder保持原样(使用代表NotificationChannel的字符串)。
  2. 注释掉if块,您可以在其中创建一个通知channel
  3. Replace NotificationManager,如下所示:

context notificationManager =NotificationManagerCompat.from(上下文);notificationManager.notify(mNotificationId,context

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52122889

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档