AVChatNotification.java 8.26 KB
Newer Older
konghaorui committed
1 2 3 4 5 6 7
package com.yidianling.avchatkit.notification;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
霍志良 committed
8

YKai committed
9
import androidx.core.app.NotificationCompat;
konghaorui committed
10

霍志良 committed
11
import com.netease.nimlib.sdk.avchat.model.AVChatData;
konghaorui committed
12 13 14
import com.yidianling.avchatkit.AVChatKit;
import com.yidianling.avchatkit.activity.AVChatActivity;
import com.yidianling.avchatkit.constant.AVChatExtras;
霍志良 committed
15
import com.yidianling.im.R;
konghaorui committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

/**
 * 音视频聊天通知栏
 * Created by huangjun on 2015/5/14.
 */
public class AVChatNotification {

    private Context context;

    private NotificationManager notificationManager;
    private Notification callingNotification;
    private Notification missCallNotification;
    private String account;
    private String displayName;
    private static final int CALLING_NOTIFY_ID = 111;
    private static final int MISS_CALL_NOTIFY_ID = 112;
霍志良 committed
32 33
    private static final int INCOMING_CALL_NOTIFY_ID = 113;
    private Notification incomingCallNotification;
konghaorui committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

    public AVChatNotification(Context context) {
        this.context = context;
    }

    public void init(String account, String displayName) {
        this.account = account;
        this.displayName = displayName;

        notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        AVChatNotificationChannelCompat26.createNIMMessageNotificationChannel(context);
    }

    private void buildCallingNotification() {
        if (callingNotification == null) {
            Intent localIntent = new Intent();
            localIntent.setClass(context, AVChatActivity.class);
            localIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

霍志良 committed
53
            String tickerText = String.format(context.getString(R.string.avchat_notification), displayName);
konghaorui committed
54 55 56 57
            int iconId = AVChatKit.getAvChatOptions().notificationIconRes;

            PendingIntent pendingIntent = PendingIntent.getActivity(context, CALLING_NOTIFY_ID, localIntent, PendingIntent
                    .FLAG_UPDATE_CURRENT);
霍志良 committed
58
            callingNotification = makeNotification(pendingIntent, context.getString(R.string.avchat_call), tickerText, tickerText,
konghaorui committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
                    iconId, false, false);
        }
    }

    private void buildMissCallNotification() {
        if (missCallNotification == null) {
            Intent notifyIntent = new Intent(context, AVChatKit.getAvChatOptions().entranceActivity);
            notifyIntent.putExtra(AVChatExtras.EXTRA_ACCOUNT, account);
            notifyIntent.putExtra(AVChatExtras.EXTRA_FROM_NOTIFICATION, true);

            notifyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            notifyIntent.setAction(Intent.ACTION_VIEW);
            notifyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, CALLING_NOTIFY_ID, notifyIntent, PendingIntent
                    .FLAG_UPDATE_CURRENT);

霍志良 committed
75
            String title = context.getString(R.string.avchat_no_pickup_call);
konghaorui committed
76
            String tickerText = displayName + ": 【网络通话】";
霍志良 committed
77
            int iconId = R.drawable.avchat_no_pickup;
konghaorui committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

            missCallNotification = makeNotification(pendingIntent, title, tickerText, tickerText, iconId, true, true);
        }
    }

    private Notification makeNotification(PendingIntent pendingIntent, String title, String content, String tickerText,
                                          int iconId, boolean ring, boolean vibrate) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, AVChatNotificationChannelCompat26.getNIMChannelId(context));
        builder.setContentTitle(title)
                .setContentText(content)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .setTicker(tickerText)
                .setSmallIcon(iconId);
        int defaults = Notification.DEFAULT_LIGHTS;
        if (vibrate) {
            defaults |= Notification.DEFAULT_VIBRATE;
        }
        if (ring) {
            defaults |= Notification.DEFAULT_SOUND;
        }
        builder.setDefaults(defaults);

        return builder.build();
    }

    public void activeCallingNotification(boolean active) {
        if (notificationManager != null) {
            if (active) {
                buildCallingNotification();
                notificationManager.notify(CALLING_NOTIFY_ID, callingNotification);
                AVChatKit.getNotifications().put(CALLING_NOTIFY_ID, callingNotification);
            } else {
                notificationManager.cancel(CALLING_NOTIFY_ID);
                AVChatKit.getNotifications().remove(CALLING_NOTIFY_ID);
            }
        }
    }

    public void activeMissCallNotification(boolean active) {
        if (notificationManager != null) {
            if (active) {
                buildMissCallNotification();
                notificationManager.notify(MISS_CALL_NOTIFY_ID, missCallNotification);
                AVChatKit.getNotifications().put(MISS_CALL_NOTIFY_ID, callingNotification);
            } else {
                notificationManager.cancel(MISS_CALL_NOTIFY_ID);
                AVChatKit.getNotifications().remove(MISS_CALL_NOTIFY_ID);
            }
        }
    }
霍志良 committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    public void activeIncomingCallNotification(boolean active, AVChatData backgroundIncomingCallData) {
        if (notificationManager != null) {
            if (active) {
                buildIncomingCallNotification(backgroundIncomingCallData);
                notificationManager.notify(INCOMING_CALL_NOTIFY_ID, incomingCallNotification);
                AVChatKit.getNotifications().put(INCOMING_CALL_NOTIFY_ID, incomingCallNotification);
            } else {
                notificationManager.cancel(INCOMING_CALL_NOTIFY_ID);
                AVChatKit.getNotifications().remove(INCOMING_CALL_NOTIFY_ID);
            }
        }
    }
    private void buildIncomingCallNotification(AVChatData backgroundIncomingCallData) {
        String displayName;
        if (AVChatKit.getUserInfoProvider() != null) {
            displayName = AVChatKit.getUserInfoProvider().getUserDisplayName(backgroundIncomingCallData.getAccount());
        } else {
            displayName = backgroundIncomingCallData.getAccount();
        }
        Intent notifyIntent = AVChatActivity.incomingCallIntent(context, backgroundIncomingCallData, displayName, AVChatActivity.FROM_BROADCASTRECEIVER);
        PendingIntent pendingIntent = PendingIntent.getActivity(
                context,
                CALLING_NOTIFY_ID,
                notifyIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        String title = "你有新的来电,请查看";
        String content = displayName + ":【网络通话】";
        String tickerText = displayName + " " + title;
        int iconId = R.drawable.avchat_imcoming_call;

        NotificationCompat.Builder incomingCallNotificationBuilder = makeNotificationBuilder(pendingIntent, title, content, tickerText, iconId, true, true);
        incomingCallNotificationBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
        incomingCallNotificationBuilder.setCategory(NotificationCompat.CATEGORY_CALL);
        incomingCallNotificationBuilder.setFullScreenIntent(pendingIntent, true);
        incomingCallNotificationBuilder.setAutoCancel(true);
        incomingCallNotification = incomingCallNotificationBuilder.build();
    }
    private NotificationCompat.Builder makeNotificationBuilder(PendingIntent pendingIntent, String title, String content, String tickerText,
                                                               int iconId, boolean ring, boolean vibrate) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, AVChatNotificationChannelCompat26.getNIMChannelId(context));
        builder.setContentTitle(title)
                .setContentText(content)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .setTicker(tickerText)
                .setSmallIcon(iconId);
        int defaults = Notification.DEFAULT_LIGHTS;
        if (vibrate) {
            defaults |= Notification.DEFAULT_VIBRATE;
        }
        if (ring) {
            defaults |= Notification.DEFAULT_SOUND;
        }
        builder.setDefaults(defaults);

        return builder;
    }

konghaorui committed
188
}