通知无法在Android Oreo中显示(API 26)

时间:2017-07-30 02:01:02

标签: android push-notification android-8.0-oreo android-8.1-oreo

尝试在Android O上显示通知时收到此消息。

  

对于卷以外的操作,不推荐使用流类型   控制

通知直接来自示例文档,并且在Android 25上显示正常。

4 个答案:

答案 0 :(得分:60)

根据this Google+ post的评论:

  

当在Android O设备上使用NotificationCompat时,预计会出现这些[警告](即使您从未传递自定义声音,NotificationCompat始终会调用setSound()。)

     

在支持库更改其代码以使用AudioAttributes setSound版本之前,您将始终收到该警告。

因此,您无法对此警告采取任何措施。根据{{​​3}},Android O根本不赞成在单个通知上设置声音,而是让您在特定类型的所有通知所使用的通知通道上设置声音。

答案 1 :(得分:43)

从Android O开始,您需要配置NotificationChannel,并在尝试显示通知时引用该频道。

private static final int NOTIFICATION_ID = 1;
private static final String NOTIFICATION_CHANNEL_ID = "my_notification_channel";

...

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_DEFAULT);

  // Configure the notification channel.
  notificationChannel.setDescription("Channel description");
  notificationChannel.enableLights(true);
  notificationChannel.setLightColor(Color.RED);
  notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
  notificationChannel.enableVibration(true);
  notificationManager.createNotificationChannel(notificationChannel);
}

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
  .setVibrate(new long[]{0, 100, 100, 100, 100, 100})
  .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
  .setSmallIcon(R.mipmap.ic_launcher)
  .setContentTitle("Content Title")
  .setContentText("Content Text");

  notificationManager.notify(NOTIFICATION_ID, builder.build());

一些重要的注意事项:

  1. NotificationChannel中指定的振动模式等设置会覆盖实际Notification中指定的振动模式。我知道,它反直觉。您应该移动将更改为通知的设置,或者为每个配置使用不同的NotificationChannel。
  2. 在您将NotificationChannel设置传递给createNotificationChannel()后,您无法修改大部分deleteNotificationChannel()设置。您甚至无法呼叫NotificationChannel,然后尝试重新添加它。使用已删除的<%= form_for :subscribe, id: 'sub_form' do |f| %> <input type="email" name="email" placeholder="Your email address" id="sub_email"> <button class="btn btn-success" type="submit" id="sub_btn">Subscribe</button><br> <p id="sub_output" class="lead" style="color: white;"></p> <% end %> 的ID将会复活它,它将与首次创建时一样不可变。在卸载应用程序之前,它将继续使用旧设置。因此,您最好确定自己的频道设置,如果您正在使用这些设置,请重新安装该应用,以使其生效。

答案 2 :(得分:8)

@ sky-kelsey所描述的一切都很好,只是轻微的补充

如果已经注册了,那么每次都不应该注册相同的频道,所以我有Utils类方法为我创建一个频道:

public static final String NOTIFICATION_CHANNEL_ID_LOCATION = "notification_channel_location";

public static void registerLocationNotifChnnl(Context context) {
    if (Build.VERSION.SDK_INT >= 26) {
        NotificationManager mngr = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        if (mngr.getNotificationChannel(NOTIFICATION_CHANNEL_ID_LOCATION) != null) {
            return;
        }
        //
        NotificationChannel channel = new NotificationChannel(
                NOTIFICATION_CHANNEL_ID_LOCATION,
                context.getString(R.string.notification_chnnl_location),
                NotificationManager.IMPORTANCE_LOW);
        // Configure the notification channel.
        channel.setDescription(context.getString(R.string.notification_chnnl_location_descr));
        channel.enableLights(false);
        channel.enableVibration(false);
        mngr.createNotificationChannel(channel);
    }
}

的strings.xml:

<string name="notification_chnnl_location">Location polling</string>
<string name="notification_chnnl_location_descr">You will see notifications on this channel ONLY during location polling</string>

在我要显示类型通知之前,我每次都会调用该方法:

    ...
    NotificationUtil.registerLocationNotifChnnl(this);
    return new NotificationCompat.Builder(this, NotificationUtil.NOTIFICATION_CHANNEL_ID_LOCATION)
            .addAction(R.mipmap.ic_launcher, getString(R.string.open_app),
                    activityPendingIntent)
            .addAction(android.R.drawable.ic_menu_close_clear_cancel, getString(R.string.remove_location_updates),
                    servicePendingIntent)
            .setContentText(text)
            ...

另一个典型问题 - 频道默认声音 - 此处描述:https://stackoverflow.com/a/45920861/2133585

答案 3 :(得分:2)

在Android O中,必须使用NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext.getApplicationContext(), "notify_001"); Intent ii = new Intent(mContext.getApplicationContext(), RootActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, ii, 0); NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle(); bigText.bigText(verseurl); bigText.setBigContentTitle("Today's Bible Verse"); bigText.setSummaryText("Text in detail"); mBuilder.setContentIntent(pendingIntent); mBuilder.setSmallIcon(R.mipmap.ic_launcher_round); mBuilder.setContentTitle("Your Title"); mBuilder.setContentText("Your text"); mBuilder.setPriority(Notification.PRIORITY_MAX); mBuilder.setStyle(bigText); NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel("notify_001", "Channel human readable title", NotificationManager.IMPORTANCE_DEFAULT); mNotificationManager.createNotificationChannel(channel); } mNotificationManager.notify(0, mBuilder.build()); 并且input已被弃用(reference)。

以下是示例代码:

<form action="tambahMatkul.php" method="post" id="ambil_matkul">  
    <table class="tabel table-bordered table-stripped table-responsive">
        <tr>
            <th width="5%">cek</th>
            <th width="19%">Kode</th>
            <th width="19%">Mata Kuliah</th>
            <th width="19%">W/P</th>
            <th width="19%">SKS</th>
            <th width="19%">Kelas</th>
        </tr>
        <?php include "koneksi.php";
        $query = $connect->query("SELECT * FROM matakuliah")or die(mysqli_error($connect));
        while($mahasiswa = $query->fetch_array()){
        ?>
        <tr>
            <td><input type="checkbox" name="cek[]" value="<?php echo $mahasiswa['kodeMatkul'] ?>" /></td>
            <td><?php echo $mahasiswa['kodeMatkul']; ?></td>
            <td><?php echo $mahasiswa['namaMatkul']; ?></td>
            <td><?php echo $mahasiswa['pilihan']; ?></td>
            <td><?php echo $mahasiswa['sks']; ?></td>
            <td><?php echo $mahasiswa['kelas']; ?></td>
        </tr>
        <?php } ?>
    </table>
    <hr/>
    <div class="btn-group"><button class="btn btn-sm btn-success" type="submit"><i class="fa fa-plus"></i> Tambah</button></div>
</form>