首页 > 解决方案 > Android SDK 在 Nougat 和 Android10 之间更改通知和警报

问题描述

我想知道 Nougat 和 Android10 之间的 Android SDK 中的通知或警报是否有任何变化。

我有一个适用于 Nougat (API 25) 的应用程序,但是当我在 Android 10 (API 29) 上对其进行测试时,它设置为发送的通知从未出现(通知由警报发送)。

注意:当它应该发送我的通知时,它会发送不同的应用程序通知(重复)。

这是我目前正在使用的:

Intent notificationIntent = new Intent(context, Reader.class);
    Bundle bundle = new Bundle();
    notificationIntent.putExtras(bundle);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.BigTextStyle bigText = new Notification.BigTextStyle();
    bigText.bigText(Text);
    bigText.setSummaryText(sText);
    Notification.Builder NotificationBuilder;
    if (android.os.Build.VERSION.SDK_INT  >= android.os.Build.VERSION_CODES.O) {
    NotificationBuilder = new Notification.Builder(context,"BN_ID");
    } else {
    NotificationBuilder = new Notification.Builder(context);
    }
    Notification.Builder mBuilder = NotificationBuilder;
    
    mBuilder.setSmallIcon(R.drawable.nicon);
    mBuilder.setContentTitle("title");
    mBuilder.setContentText(sText);
    mBuilder.setStyle(bigText);
    mBuilder.setAutoCancel(true);
    mBuilder.setContentIntent(contentIntent);
    
    mNotificationManager.notify(1, mBuilder.build());

标签: androidnotificationsandroid-alarmsandroid-version

解决方案


像这样使用正确的频道格式

import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;

public class App extends Application {

    public static final String CHANNEL_1_ID = "channel1";

    @Override
    public void onCreate() {
        super.onCreate();

        createNotificationChannels();
    }

    private void createNotificationChannels() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel1 = new NotificationChannel(
                    CHANNEL_1_ID,
                    "Channel 1",
                    NotificationManager.IMPORTANCE_HIGH
            );
            channel1.setDescription("This is channel 1");

            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel1);
        }
    }
}

并在应用程序标签下像这样在清单中添加此类

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="jeuxdevelopers.com.notifications">

    <application
        android:name=".App"

现在在你的主要活动中

public void sendOnChannel1(View view) {
    Intent activityIntent = new Intent(this, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this,
            0, activityIntent, 0);

    Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
            .setSmallIcon(R.drawable.ic_one)
            .setContentTitle("Title")
            .setContentText("text")
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_MESSAGE)
            .setContentIntent(contentIntent)
            .setAutoCancel(true)
            .setOnlyAlertOnce(true)
            .build();
    //If the first param is same it will overwrite
    notificationManagerCompat.notify(1, notification);
}

这是全局变量

private NotificationManagerCompat notificationManagerCompat;

像这样实例化它onCreate()

notificationManagerCompat = NotificationManagerCompat.from(this);

推荐阅读