FCM 推送通知在 Android 4.4 上不起作用
Posted
技术标签:
【中文标题】FCM 推送通知在 Android 4.4 上不起作用【英文标题】:FCM PUSH NOTIFICATION dosen´t work on Android 4.4 【发布时间】:2019-03-23 02:36:49 【问题描述】:我有一个应用程序,然后在为 android 8.0 编译时完美运行,在手机的通知中心获取通知..
但如果为 android 4.4 编译不起作用,(不会收到通知)
使用此文档:https://docs.microsoft.com/en-us/xamarin/android/data-cloud/google-messaging/firebase-cloud-messaging
代码 => https://github.com/franciscom95/FcmXamarinTest
【问题讨论】:
【参考方案1】:尝试浏览下面的代码希望这对你有帮助,我目前正在使用这样的代码。
public void showNotification(String message, String title, String action, String value, boolean isImageUrl, String CHANNEL_ID, String BADGE_NUMBER) throws CleverTapPermissionsNotSatisfied, CleverTapMetaDataNotFoundException
if (!TextUtils.isEmpty(message))
Intent mainIntent = new Intent(this, HomeScreen.class);
if (notification_key_values.containsKey(action.toLowerCase()))
mainIntent.putExtra("FromPushNotification", true);
mainIntent.putExtra("PushAction", action);
if (!isImageUrl)
String hash_value = notification_key_values.get(action.toLowerCase());
if (!hash_value.isEmpty())
updateNotification(message, hash_value, action, value, extras.getString("TokenId"), extras.getString("SegmentId"), extras.getString("Action"));
if (action.toLowerCase().equals("redirectionurl"))
mainIntent.putExtra("PushValue", value);
else
if (!isImageUrl)
updateNotification(message, action, action, value, "", "", "");
mainIntent.putExtra("FromPushNotification", true);
mainIntent.putExtra("PushAction", action);
else
mainIntent.putExtra("FromPushNotification", false);
mainIntent.putExtras(extras);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(mainIntent);
PendingIntent launchIntent;
launchIntent = stackBuilder.getPendingIntent((int) System.currentTimeMillis(), PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);
Notification notification;
setLargeIcon = BitmapFactory.decodeResource(this.getResources(),
R.drawable.appicon);
Uri notification_sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
CleverTapAPI cleverTapAPI = CleverTapAPI.getInstance(this);
cleverTapAPI.createNotificationChannel(getApplicationContext(), CHANNEL_ID, CHANNEL_NAME, " ", NotificationManager.IMPORTANCE_HIGH, false);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, this.CHANNEL_ID);
builder.setSmallIcon(getSmallIcon());
builder.setLargeIcon(setLargeIcon);
builder.setStyle(style);
builder.setContentTitle(title);
builder.setContentText(message);
notification = builder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.contentIntent = launchIntent;
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
notificationManagerCompat.notify((int) System.currentTimeMillis(),notification );
else
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP)
notification = new NotificationCompat.Builder(this)
.setSmallIcon(getSmallIcon())
.setLargeIcon(setLargeIcon)
.setContentTitle(
title)
.setContentText(message)
.setColor(this.getResources().getColor(R.color.position_blue))
.setStyle(style)
.setSound(notification_sound)
.setAutoCancel(true).build();
else
notification = new NotificationCompat.Builder(this)
.setSmallIcon(getSmallIcon())
.setContentTitle(
title)
.setContentText(message)
.setStyle(style)
.setSound(notification_sound)
.setAutoCancel(true).build();
NotificationCompat.BigTextStyle().bigText(message))
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.contentIntent = launchIntent;
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int) System.currentTimeMillis(), notification);
【讨论】:
感谢您的回答,它确实有帮助,但不是通知没有显示它们发出声音和振动,但没有显示在 kit kat 4.4 的通知中心,任何建议。 没有图标好像不行。 .setSmallIcon(R.drawable.small_icon);以上是关于FCM 推送通知在 Android 4.4 上不起作用的主要内容,如果未能解决你的问题,请参考以下文章
cordova-plugin-fcm 在 iOS 上不起作用
android firebase后台通知仅在vivo设备上不起作用
FCM 推送通知在 android 中不起作用(使用 cordova-plugin-fcm 2.1.1 的 Ionic 项目)