推送通知在Lollipop中不起作用

时间:2018-10-16 08:28:32

标签: android firebase push-notification

在将我的目标SDK更改为26之后,我的当前最低SDK为19,推送通知在lollipop中不起作用。但是更改为26后,它现在仅适用于O,N和M。请提出解决方案。预先感谢。

下面是我的类,该类扩展了FirebaseMessaging Service

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 :(得分:1)

答案 1 :(得分:0)

自Oreo(API 26)版本以来,您必须正在使用NotificationChannel。

如果使用了以前的版本。下一个代码参考。我的代码是

使用频道后的Oreo(API 26)版本。

不使用频道之前的Oreo(API 26)版本。

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

谢谢。