推送通知不适用于牛轧糖和奥利奥同时工作

Push notification not work for both Nougat & Oreo into same time

我有 2 个推送通知功能。

牛轧糖 (7.0)(不能高于 24)

  Uri SoundUri = RingtoneManager.getDefaultUri((RingtoneManager.TYPE_NOTIFICATION));


                    Notification notification = new Notification.Builder(first.this)
                            .setVibrate(new long[] { 350, 350})
                            .setSmallIcon(R.drawable.testicon)
                            .setContentTitle("test tittle")
                            .setContentText("test content")
                            .setSound(SoundUri).build();

                    NotificationManager notifmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                    notifmanager.notify(0,notification);

对于奥利奥或更高 + (8.0)(不适用于牛轧糖 api 24)

 CharSequence name = getString(R.string.channel_name);// The user-visible name of the channel.
        int importance = NotificationManager.IMPORTANCE_HIGH;

        String CHANNEL_ID = "my_channel_01";// The id of the channel.
        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
        int notifyID = 1;

        Notification notification = new NotificationCompat.Builder(this)
                .setVibrate(new long[] { 350, 350})
                .setSmallIcon(R.drawable.testicon)
                .setContentTitle("test tittle")
                .setChannelId(CHANNEL_ID).build();

        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        mNotificationManager.createNotificationChannel(mChannel);

        mNotificationManager.notify(notifyID , notification);

现在我用 Gradle

android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
    applicationId "com.test.testttt"
    minSdkVersion 17
    targetSdkVersion 29
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

是否有几乎所有 android 版本都接受的推送通知(方法/功能)(所以我想让它至少被接受 api +17)

检查 OS 版本是否为 Oreo 或更高版本,然后使用频道,否则不要创建频道并在通知中设置其 ID。 Oreo 及以上版本需要频道并有通知。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    notification.setChannelId(CHANNEL_ID);
}