FCM PUSH通知不适用于Android 4.4

时间:2019-03-23 02:36:49

标签: c# android xamarin push-notification firebase-cloud-messaging

我有一个应用程序,当针对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 个答案:

答案 0 :(得分:0)

尝试通过下面的代码希望对您有帮助,我目前正在这样使用。

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

}