Android通知显示随笔(已兼容到Android32)

2023-11-09  本文已影响0人  晖仔Milo

1、通知构建者

public class AiNotification {

    public NotifyChannel channel;
    public int id;
    public int priority;
    public int importance;
    public String title;
    public String text;
    public PendingIntent pendingIntent;
    public CharSequence tickerText;

    public int flags;

    public static final class AiNotificationBuilder {
        private NotifyChannel channel = NotifyChannel.Normal;

        private int priority = Notification.PRIORITY_HIGH;
        private int importance;
        private String title = "通知";
        private String text = "有一条未读消息";
        private PendingIntent pendingIntent;
        private CharSequence tickerText = "刚刚收到一条通知";
        private int flags = Notification.FLAG_AUTO_CANCEL;

        public AiNotificationBuilder() {
            if (Build.VERSION.SDK_INT >= 24) {
                importance = NotificationManager.IMPORTANCE_HIGH;
            }
        }

        public AiNotificationBuilder setImportance(int importance) {
            this.importance = importance;
            return this;
        }

        public AiNotificationBuilder setChannel(NotifyChannel channel) {
            this.channel = channel;
            return this;
        }

        public AiNotificationBuilder setPriority(int priority) {
            this.priority = priority;
            return this;
        }

        public AiNotificationBuilder setTitle(String title) {
            this.title = title;
            return this;
        }

        public AiNotificationBuilder setText(String text) {
            this.text = text;
            return this;
        }

        public AiNotificationBuilder setPendingIntent(PendingIntent pendingIntent) {
            this.pendingIntent = pendingIntent;
            return this;
        }

        public AiNotificationBuilder setTickerText(CharSequence tickerText) {
            this.tickerText = tickerText;
            return this;
        }

        public AiNotificationBuilder setFlags(int flags) {
            this.flags = flags;
            return this;
        }

        public synchronized AiNotification build() {
            AiNotification aiNotification = new AiNotification();
            NotificationFactory.NotificationId += 1;
            aiNotification.id = NotificationFactory.NotificationId;

            aiNotification.priority = this.priority;
            aiNotification.pendingIntent = this.pendingIntent;
            aiNotification.tickerText = this.tickerText;
            aiNotification.title = this.title;
            aiNotification.text = this.text;
            aiNotification.flags = this.flags;
            aiNotification.channel = this.channel;
            aiNotification.importance = this.importance;

            if (this.pendingIntent == null) {
                //显式intent
//                Intent intent = new Intent(CommonData.app.getApplicationContext(), MainActivity.class);
                //隐式intent
                Intent intent = new Intent();
                intent.setAction("aiteacher.action.MainActivity");
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                this.pendingIntent = PendingIntent.getActivity(CommonData.app.getApplicationContext(), 0, intent,
                        PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE);
                aiNotification.pendingIntent = this.pendingIntent;
            }

            return aiNotification;
        }
    }

}

2、通知制造者 + 通知显示者

public class NotificationFactory {
    private static final String TAG = "NotificationFactory";

    public static int NotificationId = 19920120;

    public static void showNotification(AiNotification aiNotification) {
        Context context = SampleApplicationLike.getInstance().getApplicationContext();
        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        Notification notification = createNotification(aiNotification);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(aiNotification.channel.channelId, aiNotification.channel.channelName,
                    aiNotification.importance);
            //锁屏显示通知
            channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
            //桌面launcher的消息角标
            channel.canShowBadge();
            nm.createNotificationChannel(channel);
        }

        nm.notify(aiNotification.id, notification);
        FZLogger.d(TAG, "showNotification, id = " + aiNotification.id);
    }

    public static Notification createNotification(AiNotification aiNotification) {
        Context context = SampleApplicationLike.getInstance().getApplicationContext();
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, aiNotification.channel.channelId);

        builder.setContentTitle(aiNotification.title)
                .setContentText(aiNotification.text)
                .setContentIntent(aiNotification.pendingIntent)
                .setFullScreenIntent(aiNotification.pendingIntent, true)
                .setSmallIcon(R.drawable.push_small)
                .setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setPriority(aiNotification.priority)
                .setCategory(Notification.CATEGORY_MESSAGE)
                .setWhen(System.currentTimeMillis())
                .setTicker(aiNotification.tickerText);

        return builder.build();
    }


}

3、调用者

NotificationFactory.showNotification(new AiNotification.AiNotificationBuilder().build());

4、另外附上隐私Activity的声明

        <activity
            android:name=".ui.MainActivity"
            android:exported="false"
            android:screenOrientation="portrait"
            android:taskAffinity=""
            android:excludeFromRecents="true">
            <intent-filter>
                <action android:name="aiteacher.action.MainActivity" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
上一篇下一篇

猜你喜欢

热点阅读