Push-уведомление не работает в Lollipop - PullRequest
0 голосов
/ 16 октября 2018

Push-уведомление не работает в lollipop после изменения моего целевого SDK на 26, мой текущий минимальный SDK - 19. Он работал нормально раньше.Но после изменения на 26 он теперь работает только в O, N и M. Пожалуйста, предложите мне решение.Заранее спасибо.

Ниже приведен мой класс, который расширяет службу FirebaseMessaging

FireMsgService.java

 @Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//        super.onMessageReceived(remoteMessage);
    Log.e("Msg", "Message received [" + remoteMessage + "]");
    Intent intent = new Intent(this, HomeActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 1410,
            intent, PendingIntent.FLAG_ONE_SHOT);
    context = getApplicationContext();
    String message = "";
    String type = "";

    try {
        JSONObject jsonObject = new JSONObject(remoteMessage.getData());
        if (jsonObject.has("id"))
            message = (String) jsonObject.get("id");
        if (jsonObject.has("type"))
            type = (String) jsonObject.get("type");
        if (jsonObject.has("room_id"))
            message = (String) jsonObject.get("room_id");
        if (jsonObject.has("type"))
            type = (String) jsonObject.get("type");
        saveTypeId(type,message);
        if (jsonObject.has("badge")) {
            badge = (int) jsonObject.get("badge");
            Log.d("BadgeCount",badge+"");
        }

        Log.e("","------> "+message);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    NotificationCompat.Builder notificationBuilder = new
            NotificationCompat.Builder(this)
            .setContentTitle(remoteMessage.getNotification().getTitle())
            .setContentText(remoteMessage.getNotification().getBody())
            .setNumber(badge);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        notificationBuilder.setSmallIcon(R.drawable.ic_new_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
                        R.mipmap.ic_launcher))
                .setColor(ContextCompat.getColor(context,R.color.blue));
    } else {
        notificationBuilder.setSmallIcon(R.drawable.ic_launcher);
    }
    notificationBuilder.setAutoCancel(true)
            .setVibrate(new long[0])
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setContentIntent(pendingIntent);

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

    notificationManager.notify(1410, notificationBuilder.build());

}

Вот мой файл build.gradle

build.gradle

android {
compileSdkVersion 26
buildToolsVersion '26.0.0'

defaultConfig {
    applicationId "com.abc.xyz"
    minSdkVersion 19
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    multiDexEnabled true
}
buildTypes {
    release {
        minifyEnabled false
        multiDexEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 
'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support:cardview-v7:26.1.0'
compile 'com.android.support:design:26.1.0'
compile 'com.android.support.constraint:constraint-layout:1.1.3'
compile 'com.google.android.gms:play-services-location:11.0.4'
compile 'com.google.android.gms:play-services-maps:11.0.4'
compile 'com.google.android.gms:play-services-auth:11.0.4'
compile 'com.google.android.gms:play-services-places:11.0.4'
compile 'com.google.firebase:firebase-messaging:11.0.4'
compile 'com.google.firebase:firebase-messaging:11.0.4'
}
apply plugin: 'com.google.gms.google-services'

Ответы [ 2 ]

0 голосов
/ 16 октября 2018

Oreo (API 26) Версия с тех пор, вы должны использовать NotificationChannel.

, если до использования версии.следующий кодМой код:

Oreo (API 26) Версия с момента использования Channel.

Oreo (API 26) Версия перед тем, как не использовать Channel.

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel mChannel = new NotificationChannel(
                "channel", "channel", NotificationManager.IMPORTANCE_HIGH);
        notificationManager.createNotificationChannel(mChannel);
        notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), mChannel.getId());
    }else {
        notificationBuilder = new NotificationCompat.Builder(getApplicationContext());
    }

Спасибо.

0 голосов
/ 16 октября 2018

Начиная с API 26, вы должны указать ChannelId для конструктора Notification.Builder: https://developer.android.com/reference/android/app/Notification.Builder.html#Notification.Builder(android.content.Context)

Дополнительная информация о каналах: https://developer.android.com/training/notify-user/channels

...