通知未显示安卓



我目前正在尝试使用 FCM 为我的安卓应用程序实现推送通知,但是当我发送通知时,它不会出现在手机上。我确定该应用程序收到了该消息,因为它出现在logcat中,但是通知本身没有出现,我不确定我哪里出错了。我从滑行中得到一个错误,但我在没有大图标和滑行线外的.notify线的情况下再次尝试,但仍然没有任何出现。任何帮助都值得赞赏:)

Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Spannable sb = new SpannableString("Switcheroo");
sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.app_icon_waves_round)
.setContentTitle(sb)
.setContentText(message)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Glide.with(getApplicationContext()).asBitmap().load(image).placeholder(R.drawable.music_placeholder).into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
notificationBuilder.setLargeIcon(resource);
notificationManager.notify(0, notificationBuilder.build());
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});

日志猫:

D/PAYLOAD: Message data payload: {image=https://is1-ssl.mzstatic.com/image/thumb/WNUBiv2P6YSklHn9eA5nlg/1000x1000bb.jpeg, message=Barbecu sent you his "Run The Jewels" Playlist}
W/Glide: Failed to find GeneratedAppGlideModule. You should include an annotationProcessor compile dependency on com.github.bumptech.glide:compiler in your application and a @GlideModule annotated AppGlideModule implementation or LibraryGlideModules will be silently ignored

从 Android 8.0(API 级别 26(开始,除非您将NotificationChannel通知作为NotificationCompat.Builder构造函数的第二个参数提供,否则不会显示通知。

因此,要解决这个问题,请为 API 26+ 创建一个NotificationChannel


// Notification channels are only available in OREO and higher.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel
("PRIMARY_CHANNEL_ID",
"Service",
NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setDescription("Description");
notificationManager.createNotificationChannel(notificationChannel);
}

使用通道 ID "PRIMARY_CHANNEL_ID"作为NotificationCompat.Builder构造函数的第二个参数。

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "PRIMARY_CHANNEL_ID")
.setSmallIcon(R.mipmap.app_icon_waves_round)
.setContentTitle(sb)
.setContentText(message)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent);

当你在应用程序中使用 Glide4 时,你必须制作这样的类。

import android.content.Context;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.module.AppGlideModule;
@GlideModule
public final class MyAppGlideModule extends AppGlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
}
}

检查这个。祝您编码愉快!

最新更新