通知未出现 Android

Notification Not Appearing Android

我目前正在尝试使用 FCM 为我的 android 应用实现推送通知,但是当我发送通知时,它没有出现在 phone 上。我确定该应用收到了消息,因为它出现在 logcat 中,但通知本身没有出现,我不确定哪里出错了。我从 Glide 得到一个错误,但我在没有 LargeIcon 和 .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) {
            }
        });

LogCat:

 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

当您在应用中使用 Glide4 时。你必须像这样class。

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) {

    }
}

检查 this。编码愉快!

从 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);