简体   繁体   English

推送通知在Lollipop中不起作用

[英]Push notification is not working in Lollipop

Push notification is not working in lollipop after changing my target SDK to 26, my current minimum SDK is 19. It was working fine before. 将我的目标SDK更改为26之后,推送通知在lollipop中不起作用,我当前的最低SDK为19。之前运行良好。 But after changing to 26, it's now working only in O, N, and M. Please suggest me a solution. 但是更改为26后,它现在仅适用于O,N和M。请提出解决方案。 Thanks in advance. 提前致谢。

Below is my class which extends FirebaseMessaging Service 下面是我的类,它扩展了FirebaseMessaging Service

FireMsgService.java 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());

}

Here is my build.gradle file 这是我的build.gradle文件

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'

Since API 26, you have to specify a ChannelId to the Notification.Builder constructor : https://developer.android.com/reference/android/app/Notification.Builder.html#Notification.Builder(android.content.Context) 从API 26开始,您必须为Notification.Builder构造函数指定一个ChannelIdhttps : //developer.android.com/reference/android/app/Notification.Builder.html#Notification.Builder(android.content.Context)

More information about Channels : https://developer.android.com/training/notify-user/channels 有关渠道的更多信息: https : //developer.android.com/training/notify-user/channels

Oreo (API 26) Version since, You must be using NotificationChannel. 从Oreo(API 26)版本开始,您必须使用NotificationChannel。

if before version used. 如果使用以前的版本。 next code Reference. 下一个代码参考。 My Code is 我的代码是

Oreo (API 26) Version since use Channel. 自使用频道以来的Oreo(API 26)版本。

Oreo (API 26) Version before not use 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());
    }

Thank. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM