android - 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());
解决方案
像这样使用正确的频道格式
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);
推荐阅读
- java - 在 Eclipse 中未收到“添加未实现的方法”错误
- firebase - 为什么 Firestore timestamp.toDate() 在云函数中将日期添加 4 小时,但在浏览器上,它会正确获取本地时间?
- java - Mysql Connection 正在返回“null”。我无法连接mysql数据库
- machine-learning - RankLib 中的 k 折交叉验证
- sql - 如何找到具有进入状态而没有退出状态的行
- jquery - 如何使用jquery将逗号分隔值从视图发送到控制器
- c - 为什么我不能在我的代码中添加其他关于给学生评分的字符?
- coq - 检查自然数的后继
- sql - 如何在 SQL 中对列进行 GROUP BY 组合?
- python - numpy 与 Python map() 中的矢量化