- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是开发应用程序的新手。目前,我正在研究推送通知。在此之前,我尝试使用 Android 4.4,推送通知工作得很好。但是,现在我尝试在 Android 9(Pie)上进行调试,但似乎没有出现通知。
我已经尝试了一些解决方案。他们建议对 Android 8 及更高版本使用通知 channel 。我已经尝试了这里的一些解决方案:Previous question answer在这里 Previous question answer 。但是,我不知道为什么它对我不起作用。
这是我的代码
public void onReceive(final Context context, final Intent intent) {
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int notificationId = 1;
String channelId = "channel-01";
String channelName = "Channel Name";
int importance = NotificationManager.IMPORTANCE_HIGH;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(
channelId, channelName, importance);
nm.createNotificationChannel(mChannel);
}
if (!intent.getExtras().getBoolean("cancel", false)) {
this.context = context;
prefs = context.getSharedPreferences("prefs", Context.MODE_PRIVATE);
notification = new NotificationCompat.Builder(context, channelId);
count = intent.getExtras().getInt("count");
name = prefs.getString("name"+count, "");
hours = prefs.getInt("hora"+count, 8);
minutes = prefs.getInt("minuto"+count, 0);
minutesBefore = prefs.getInt("minutesBefore", 30);
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT_WATCH)
isScreenOn = pm.isInteractive();
else
isScreenOn = pm.isScreenOn();
if (!isScreenOn) {
@SuppressLint("InvalidWakeLockTag") PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "MyLock");
wl.acquire(10000);
@SuppressLint("InvalidWakeLockTag") PowerManager.WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyCpuLock");
wl_cpu.acquire(10000);
}
/**
* notification
*/
final Intent notificationIntent = new Intent(context, Broadcast_TakenAction.class);
//Broadcast_TakenAction gets called when notification is clicked
notificationIntent.putExtra("count", count);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pIntent = PendingIntent.getBroadcast(context, count, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setAutoCancel(true);
notification.setLargeIcon(drawableToBitmap(ResourcesCompat.getDrawable(context.getResources(), R.drawable.notification_large_icon, null)));
notification.setSmallIcon(R.drawable.small_icon);
notification.setWhen(System.currentTimeMillis());
notification.setContentTitle(name);
notification.setContentIntent(pIntent);
notification.setPriority(0);
notification.addAction(R.drawable.check, "Taken", pIntent);
if (intent.getExtras().getBoolean("shownBefore", false)) {
Runnable delayedThreadStartTask2 = new Runnable() {
@Override
public void run() {
new Thread(
new Runnable() {
@Override
public void run() {
for (int incr = 0; incr < minutesBefore; incr++) {
if (!intent.getExtras().getBoolean("cancel", false)) {
int time_left = minutesBefore - incr;
notification.setContentText(time_left + "m left.");
nm.notify(count, notification.build());
try {
Thread.sleep(60 * 1000);
} catch (InterruptedException ignored) {
}
}
}
realNotification();
if (prefs.getBoolean("vibrates", true)) {
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(500);
}
}
}
).start();
}
};
delayedThreadStartTask2.run();
} else {
realNotification();
}
}
}
我不确定我的代码有什么问题。如果我错了,请纠正我。
最佳答案
此代码在任何 API 编号中都适用于我:
public void Notificate() {
try {
final String NOTIFICATION_CHANNEL_ID = "10001";
String notification_title = getResources().getString(R.string.label);
String notification_message =jsonArray.get(4).getAsString() ;
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(ActivityMain.this)
.setSmallIcon(R.drawable.tahlil)
.setContentTitle(notification_title)
.setContentText(notification_message)
.setAutoCancel(true)
.setVibrate(new long[]{100, 200, 300, 400})
.setSound(alarmSound);
Intent resultIntent = new Intent(getApplicationContext(), ActivityChatList.class);
resultIntent.putExtra("menuFragment", "favoritesMenuItem");
// resultIntent.putExtra("user_id", from_user_id);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
getApplicationContext(),
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT
);
mBuilder.setContentIntent(resultPendingIntent);
int mNotificationId = System.currentTimeMillis();
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
if (mNotifyMgr != null) {
mNotifyMgr.createNotificationChannel(notificationChannel);
}
}
if (mNotifyMgr != null) {
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
} catch (Exception e) {
}
}
关于java - NotificationChannel 不适用于 Android 8 及更高版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61569246/
在 Android 中设置通知时,从 Oreo 开始,您还需要设置 NotificationChannel 为了设置它,您需要给它一个 name: String和 description: Strin
我在播放声音时遇到问题,当通知到来时,通知正在显示,但没有声音或振动,我看到很多关于如何将声音和振动设置为通知 channel 的问答,但没有行不通,这就是我正在尝试的方式 val pattern =
我将 NotificationCompat.Builder 设置为: .setSound(getNotificationSound(), AudioManager.STREAM_ALARM) .set
我最近在我的通知类别中添加了一个类别,并且我能够将我想要的内容设置到该类别中。然而,另一个类别显示在“未分类”下的这些类别列表中,我相信它是默认类别,如下图: 知道如何删除该类别吗? 如果删除它是不好
我正在尝试使用 Android Oreo (SDK 27) 处理通知。 这是我创建 NotificationChannel 的代码: NotificationManagerCompat notific
今天我开始以 API 26 为目标,这迫使我使用通知 channel 。 我的问题是现在每个新通知(包括更新)都会播放令人讨厌的声音。 如何禁用此声音? 我尝试用自定义的 mp3 声音替换此声音,以便
解释: 我通过 fcm 发送消息。 当用户收到消息时,会显示通知并播放特定的(在应用程序原始文件夹中的多个音频文件中)音频文件。 要播放不同的音频消息,我只需在下面的代码中更改 jUri 中提供的文件
我在运行 Android Oreo 的设备中遇到 NotificationChannel 声音的奇怪问题。 假设我的项目原始目录中有两个声音文件: a.mp3 c.mp3 然后在第一次安装应用程序时,
我已经将 Android Studio 更新到 3.0 Beta 5 NotificationManager 等其他类也可以正常导入。但它在任何地方都找不到 NotificationChannel。
我是开发应用程序的新手。目前,我正在研究推送通知。在此之前,我尝试使用 Android 4.4,推送通知工作得很好。但是,现在我尝试在 Android 9(Pie)上进行调试,但似乎没有出现通知。 我
我在尝试编译我的 android React-native 应用程序时遇到以下错误。 :react-native-push-notification:compileReleaseJavaWit
我将我的代码更新到 API v26,设置了 NotificationChannels,我可以看到我的通知,但我有关于禁用通知的逻辑。在 26 岁之前,我有类似的东西: NotificationMana
在使用 Android 消息 2.3.063 版发送彩信时,我收到一个 toast “开发者警告包 com.google.android.apps.messaging”。 在日志中 08-12 16:
我想在android API = android.os.Build.VERSION_CODES.O的if语句 但是它不能在低于26的API上运行。我们需要它才能在API 18上运行。 导入的软件包:
如何从特定的应用程序通知 channel 获取以下详细信息 获取声音();获取名称();获取标识符(); getGroup(); 我尝试使用以下代码,但它返回系统默认 channel 详细信息 Ur
我已将目标构建版本升级到27,因此需要实现通知 channel 。我正在启动一个前台 service,它将在启动时显示通知。通过通知 channel ,它按预期工作,但它总是发出声音。更改通知 pri
我正在尝试在 Android 上设置 Firebase 云消息传递客户端应用程序。我正在使用 https://github.com/firebase/quickstart-android/tree/m
我有自定义声音的推送通知,直到 android 10。从 Android 11 开始,当通知显示为下拉样式时,附加到通知 channel 的声音停止播放。当它显示为全屏事件时,它会起作用。 这是如何创
我有一个非常基本的 Windows Phone Silverlight 8.1 应用程序,其中包含以下内容(我想在将其添加到更大的现有应用程序之前证明这个概念): HttpNotificationCh
我是一名优秀的程序员,十分优秀!