gpt4 book ai didi

android - Oreo 8.0 中的推送通知问题

转载 作者:行者123 更新时间:2023-11-29 15:36:31 27 4
gpt4 key购买 nike

将应用程序更新到 8.1 后,通知未显示,我修复了它。现在,挂起的 Intent 没有按预期工作。

收到通知后,如果应用程序在后台,我将无法导航到该应用程序,如果它已关闭,则无法启动。

private void sendNotify(String messageBody) {
Intent intent = new Intent();
intent.setAction(Constants.NOTIFY);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

int uniqueId = (int) System.currentTimeMillis();

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

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

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Creates the PendingIntent
PendingIntent notifyPendingIntent =
PendingIntent.getActivity(
this,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);

String channelID = "com.myapp.ind.push.ServiceListener";// The id of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(channelID, "MyApp", importance);
// Create a notification and set the notification channel.
Notification notification = getNotificationBuilder(messageBody, notifyPendingIntent, defaultSoundUri)
.setChannelId(channelID)
.build();

if (notificationManager != null) {
notificationManager.createNotificationChannel(mChannel);
notificationManager.notify(uniqueId, notification);
}
} else if (notificationManager != null) {
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
uniqueId /* Request code */,
intent,
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = getNotificationBuilder(messageBody, pendingIntent, defaultSoundUri);
notificationManager.notify(uniqueId /* ID of notification */,
notificationBuilder.build());
}
}

private NotificationCompat.Builder getNotificationBuilder(String messageBody, PendingIntent pendingIntent, Uri defaultSoundUri) {
return new NotificationCompat.Builder(this)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle(getString(R.string.notification_title))
.setContentText(messageBody)
.setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
}

编辑:

点击通知它在 6.0 之前工作正常。更新到 8.0 后,它无法在 Google Pixel 设备上运行。它不会打开应用程序或将应用程序带到前台。

最佳答案

像下面这样创建一个 Receiver 类。并注册为 list 文件。

public class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() != null
&& intent.getAction().equals("My Call")) {
Intent startIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
context.startActivity(startIntent);
}
}
}

收到通知后来自 Notification 类的以下代码。

private void sendNotification() {
Intent intent = new Intent(this, Receiver.class);
intent.setAction("My Call");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

int uniqueId = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
uniqueId /* Request code */,
intent,
PendingIntent.FLAG_ONE_SHOT);

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && notificationManager != null) {
String channelID = "Your Channel ID";// The id of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(channelID, "My_Name", importance);
// Create a notification and set the notification channel.
Notification notification = getNotificationBuilder(messageBody, pendingIntent, defaultSoundUri)
.setChannelId(channelID)
.build();

notificationManager.createNotificationChannel(mChannel);
notificationManager.notify(uniqueId, notification);
} else if (notificationManager != null) {
NotificationCompat.Builder notificationBuilder = getNotificationBuilder();
notificationManager.notify(uniqueId /* ID of notification */,
notificationBuilder.build());
}
}

private NotificationCompat.Builder getNotificationBuilder() {
return new NotificationCompat.Builder(this)
.setSmallIcon(image)
.setContentTitle(title)
.setContentText(message)
.setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
}

现在我在所有设备上都收到了通知。由于 Oreo 设备中的 Channel 实现,我之前也收到但未显示在状态栏中。现在完美运行。

关于android - Oreo 8.0 中的推送通知问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48645550/

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