Skip to content

Instantly share code, notes, and snippets.

@r17171709
Created October 9, 2015 08:34
Show Gist options
  • Save r17171709/13a74bbd5be514b5be28 to your computer and use it in GitHub Desktop.
Save r17171709/13a74bbd5be514b5be28 to your computer and use it in GitHub Desktop.
notificationCompat的使用
private void showNotification() {
NotificationCompat.Builder build=new NotificationCompat.Builder(this);
build.setSmallIcon(R.mipmap.ic_launcher);
build.setTicker("这是通知");
build.setContentTitle("这是标题");
//设置优先级
build.setPriority(NotificationCompat.PRIORITY_MIN);
build.setContentText("这是内容");
build.setContentIntent(getPendingIntent("普通文字", 0));
//大图模式
NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle();
style.bigPicture(BitmapFactory.decodeResource(getResources(),
R.mipmap.example_big_picture));
style.setBigContentTitle("这是大标题");
style.setSummaryText("这是大内容");
build.setStyle(style).setDeleteIntent(getPendingIntent("删除", 3));
//底部action
build.addAction(R.mipmap.ic_launcher, "action1", getPendingIntent("action1", 1)).addAction(R.mipmap.ic_launcher, "action2", getPendingIntent("action2", 2));
//多文字模式
// NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle();
// style.bigText("瑞典学院常任秘书长萨拉·丹尼尔斯宣布白俄罗斯作家斯维特拉娜·阿列克谢耶维奇(Svetlana Aleksijevitj )获得2015年诺贝尔文学奖。\n" +
// "颁奖词:她的复调作品是对我们时代的磨难与勇气的纪念。\n");
// style.setBigContentTitle("这是大标题");
// style.setSummaryText("这是大内容");
// build.setStyle(style);
NotificationManager manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(1000, build.build());
}
public PendingIntent getPendingIntent(String text, int i) {
Intent intent=new Intent(this, NothingActivity.class);
Bundle bundle=new Bundle();
bundle.putString("text", text);
intent.putExtras(bundle);
//此处requestCode务必不同,不然会造成notification更新失败
PendingIntent pendingIntent=PendingIntent.getActivity(this, i, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment