当前位置: 移动技术网 > 移动技术>移动开发>Android > LeanCloud集成聊天没有推送通知问题

LeanCloud集成聊天没有推送通知问题

2020年08月14日  | 移动技术网移动技术  | 我要评论
前言集成LeanCloud即时通讯集成官方的UI组件,发现收发信息正常但是无法收到通知,于是攻克一下。步骤首先进行debug调试发现进入,但是执行到LCIMNotificationUtils.showNotification()方法发现无法进行创建,原来是NotificationCompat.Builder()过时,失效看来官方的组件也有段日子没有更新了,Android O 版本后,该方法被以下方法取代:NotificationCompat.Builder(Contex.

前言

集成LeanCloud即时通讯集成官方的UI组件,发现收发信息正常但是无法收到通知,于是攻克一下。

 

步骤

首先进行debug调试发现进入,但是执行到

LCIMNotificationUtils.showNotification()方法发现无法进行创建,原来是NotificationCompat.Builder()过时,失效

看来官方的组件也有段日子没有更新了,

Android O 版本后,该方法被以下方法取代:

NotificationCompat.Builder(Context context, String channelId)

调整 LCIMNotificationUtils.showNotification()方法 我把整个类都粘出来方便大家使用

package cn.leancloud.chatkit.utils;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;

import java.util.LinkedList;
import java.util.List;

import androidx.core.app.NotificationCompat;

import cn.leancloud.chatkit.R;

import static android.content.Context.NOTIFICATION_SERVICE;

/**
 * Created by wli on 15/8/26.
 * Notification 相关的 Util
 */
public class LCIMNotificationUtils {

    private static int lastNotificationId = 0;

    /**
     * tag list,用来标记是否应该展示 Notification
     * 比如已经在聊天页面了,实际就不应该再弹出 notification
     */
    private static List<String> notificationTagList = new LinkedList<String>();

    /**
     * 添加 tag 到 tag list,在 MessageHandler 弹出 notification 前会判断是否与此 tag 相等
     * 若相等,则不弹,反之,则弹出
     *
     * @param tag
     */
    public static void addTag(String tag) {
        if (!notificationTagList.contains(tag)) {
            notificationTagList.add(tag);
        }
    }

    /**
     * 在 tag list 中 remove 该 tag
     *
     * @param tag
     */
    public static void removeTag(String tag) {
        notificationTagList.remove(tag);
    }

    /**
     * 判断是否应该弹出 notification
     * 判断标准是该 tag 是否包含在 tag list 中
     *
     * @param tag
     * @return
     */
    public static boolean isShowNotification(String tag) {
        return !notificationTagList.contains(tag);
    }

    public static void showNotification(Context context, String title, String content, Intent intent) {
        showNotification(context, title, content, null, intent);
    }

    public static void showNotification(Context context, String title, String content, String sound, Intent intent) {
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);

        NotificationManager manager =
                (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

        NotificationChannel notificationChannel = null;
        NotificationCompat.Builder mBuilder = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            notificationChannel = new NotificationChannel("default", "默认通知", NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel(notificationChannel);
          mBuilder = new NotificationCompat.Builder(context, "default")
                  .setSmallIcon(context.getApplicationInfo().icon)
                  .setContentTitle(title).setAutoCancel(true).setContentIntent(contentIntent)
                  .setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
                  .setContentText(content);
        }else {
          mBuilder = new NotificationCompat.Builder(context)
                  .setSmallIcon(context.getApplicationInfo().icon)
                  .setContentTitle(title).setAutoCancel(true).setContentIntent(contentIntent)
                  .setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
                  .setContentText(content);
        }


        Notification notification = mBuilder.build();
        if (sound != null && sound.trim().length() > 0) {
            notification.sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + sound);
        }
        lastNotificationId = (lastNotificationId > 10000 ? 0 : lastNotificationId + 1);
        manager.notify(lastNotificationId, notification);
    }
}

用户体系问题

但是发现发消息还是没有通知,最后发现leanCloud uikit中需要自己去维护一套用户体系

在LCIMMessageHandler中sendNotification方法 userProfile为空时则不显示通知栏

else if (null != userProfile) {
           String title = userProfile.getName();
           Intent intent = getIMNotificationIntent(conversation.getConversationId(), message.getFrom());
           LCIMNotificationUtils.showNotification(context, title, notificationContent, null, intent);
          }

那么userProfile到底是什么 有从哪里来呢?

有兴趣的可以查看LCIMProfileCache类,其实很就是维护了一套缓存,如果用户不存在缓存中自然没有userProfile,所以解决办法就是,在适当的时机(例如启动APP)获取当前用户的相关用户信息并缓存其中。

缓存添加用户使用方式

LCIMProfileCache.getInstance().cacheUser(new LCChatKitUser(conv.getUser().getId() + "", conv.getUser().getName(), conv.getUser().getAvatar()));

这里只是举个例子,根据自己情况构造LCChatKitUser并存入即可。

 

再测试发现聊天就有推送通知了。

 

参考文章:

NotificationCompat.Builder()过时,失效

本文地址:https://blog.csdn.net/knockheart/article/details/107932431

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网