Android_基础 Notification


发送一个普通的通知

@SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    public static void notify(Context context, int id, String title,
            String content, PendingIntent deletePendingIntent,
            PendingIntent contentPendingIntent) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            // API16+
            NotificationManager notificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notification = new Notification.Builder(context)
                    .setContentTitle(title)
                    .setContentText(content)
                    .setWhen(System.currentTimeMillis()) 
                    .setSmallIcon(R.drawable.ic_transparent)//不需要小图标,设置一个透明
                    .setShowWhen(false)
                    .setDeleteIntent(deletePendingIntent)
                    .setContentIntent(contentPendingIntent)
                    .setAutoCancel(true) // 设置点击自动消失
                    .setSound(             // 设置铃声默认设置
                            RingtoneManager.getActualDefaultRingtoneUri(
                                    context, RingtoneManager.TYPE_NOTIFICATION))
                    .setLargeIcon(    
                            BitmapFactory.decodeResource(
                                    context.getResources(),
                                    R.drawable.icon)).build();
            
            notificationManager.notify(id, notification);
        } else {
            // API11-15
            NotificationManager manager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            Notification.Builder builder = new Notification.Builder(context)
                    .setContentTitle(title)
                    .setContentText(content)
                    .setContentIntent(contentPendingIntent)
                    .setAutoCancel(true) // 设置点击自动消失
                    .setSound(             // 设置铃声默认设置
                            RingtoneManager.getActualDefaultRingtoneUri(
                                    context, RingtoneManager.TYPE_NOTIFICATION))
                    .setSmallIcon(R.drawable.icon)
                    
                    .setWhen(System.currentTimeMillis()).setOngoing(true);
            Notification notification = builder.getNotification();
            notification.flags = Notification.FLAG_ONLY_ALERT_ONCE;
            manager.notify(id, notification);
        }
    }



                Intent intent = new Intent(mContext,MainActivity.class);
                PendingIntent pendingIntent = PendingIntent.
                        getActivity(mContext,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
                NotificationUtils.notify(mContext,100,"标题","内容",pendingIntent,pendingIntent);
  


发送一个自定义界面的通知,使用广播来处理点击事件

    public static void notifyCustomView(Context context, int id, PendingIntent deletePendingIntent,
                                        PendingIntent contentPendingIntent,
                                        RemoteViews remoteViews) {
        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification.Builder(context)
                .setSmallIcon(R.drawable.ic_ic)
                .setDeleteIntent(deletePendingIntent)
                .setContentIntent(contentPendingIntent)
                .setContent(remoteViews)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_ic))
                .build();
        notificationManager.notify(id, notification);
    }
                RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.notification);
                remoteViews.setImageViewResource(R.id.notification_iv,R.drawable.ic_ic);
                remoteViews.setTextViewText(R.id.notification_tv_title,"标题");
                remoteViews.setTextViewText(R.id.notification_tv_content,"内容");

                // 点击发送发送广播
                Intent broadcastIntent = new Intent("com.ztt.action.click");
                PendingIntent pendingIntentBroadcast = PendingIntent.getBroadcast(
                        mContext,100,broadcastIntent,PendingIntent.FLAG_CANCEL_CURRENT);
                remoteViews.setOnClickPendingIntent(R.id.notification_iv,pendingIntentBroadcast);

                Intent intent = new Intent(mContext,MainActivity.class);
                PendingIntent pendingIntent = PendingIntent.
                        getActivity(mContext,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);

                NotificationUtils.notifyCustomView(mContext,100,pendingIntent,pendingIntent,remoteViews);







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值