gpt4 book ai didi

java - NotificationChannel 不适用于 Android 8 及更高版本

转载 作者:行者123 更新时间:2023-12-01 16:57:24 26 4
gpt4 key购买 nike

我是开发应用程序的新手。目前,我正在研究推送通知。在此之前,我尝试使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com