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);
}
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页