Android notification

1.悬挂式Notification

public void suspendingNotification(Context context, String title, String content) {
    Log.w(TAG, "show ui update notification");
    mContext = context;

    NotificationManager manager = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);
    //NormalIntent
    Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(mContext.getPackageName());
    //PendingIntent
    PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);

    Notification notification;
    Notification.Builder builder = new Notification.Builder(mContext)
            .setContentTitle(title)
            .setContentText(content)
            .setSmallIcon(R.drawable.icon)
            .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.icon));
    //Suspending
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Log.w(TAG, "show hanging notification");
        builder.setContentText(content)
                .setFullScreenIntent(pendingIntent, true);
    }
    notification = builder.build();
    //NotificationIntent
    notification.contentIntent = pendingIntent;
    //ClearWhenClicked
    notification.flags = Notification.FLAG_AUTO_CANCEL;
    manager.notify(0, notification);
}

2. 普通Notification

public void normalNotification(Context context, String title, String content) {
    mContext = context;

    NotificationManager manager = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);
    //NormalIntent
    Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(mContext.getPackageName());
    //PendingIntent
    PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);

    Notification notification = new Notification.Builder(mContext)
            .setContentTitle(title)
            .setContentText(content)
            .setSmallIcon(R.drawable.icon)
            .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.icon)).build();

    notification.contentIntent = pendingIntent;

    manager.notify(0, notification);
}

3. 自定义布局Notification

public void customNotification(Context context, String packageName, String title, String content) {
    mContext = context;
    Intent intent;

    NotificationManager manager = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);
    RemoteViews remoteViews = new RemoteViews(packageName, R.layout.notification);
    remoteViews.setTextViewText(R.id.title, title);
    remoteViews.setTextViewText(R.id.content, content);


    intent = new Intent();
    intent.setClass(mContext, MainActivity.class);
    intent.putExtra("target", "ok");
    PendingIntent okIntent = PendingIntent.getActivity(mContext, R.id.sml, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.sml, okIntent);

    intent = new Intent();
    intent.setClass(mContext, MainActivity.class);
    intent.putExtra("target", "cancel");
    PendingIntent cancelIntent = PendingIntent.getActivity(mContext, R.id.cancel, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.cancel, cancelIntent);


    Notification notification = null;
    Notification.Builder builder = new Notification.Builder(mContext)
            .setSmallIcon(R.drawable.icon)
            .setPriority(Notification.PRIORITY_DEFAULT);
    builder.setContent(remoteViews);

    notification = builder.build();
    if (android.os.Build.VERSION.SDK_INT >= 16) {
        notification.bigContentView = remoteViews;
    }


    manager.notify(1, notification);
}

4. 展开/收缩Notification

public void inboxNotification(Context context, String packageName, String title, String content) {
    mContext = context;
    Notification.Builder mBuilder =
            new Notification.Builder(mContext)
                    .setSmallIcon(R.drawable.icon)
                    .setContentTitle(title)
                    .setContentText(content);

    RemoteViews remoteViews = new RemoteViews(packageName, R.layout.activity_wide);

    Notification notification = null;
    notification = mBuilder.build();
    if (android.os.Build.VERSION.SDK_INT >= 16) {
        notification = mBuilder.build();
        notification.bigContentView = remoteViews;
    }
    remoteViews = new RemoteViews(packageName, R.layout.activity_slim);
    notification.contentView = remoteViews;

    NotificationManager mNotificationManager =
            (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mNotificationManager.notify(1, notification);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值