首页 > 其他分享 >Notification的基本用法

Notification的基本用法

时间:2023-04-06 21:36:31浏览次数:37  
标签:基本 Notification 用法 FLAG intent notification Intent store


android4.0以前:

private static final int NOTIFY_ID = 0;
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
	private void showNotification(Store store) {
		Notification notification = new Notification();
		notification.flags |= Notification.FLAG_SHOW_LIGHTS;
		notification.flags |= Notification.FLAG_AUTO_CANCEL;
		notification.defaults = Notification.DEFAULT_ALL;
		notification.icon = R.drawable.ic_launch;
		notification.when = System.currentTimeMillis();

		Intent intent = new Intent(this,AlarmActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
		intent.putExtra("store", store);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);//FLAG_ONE_SHOT

		 //Change the name of the notification here
		notification.setLatestEventInfo(this, store.getStoreName()+"("+store.getDistance()+")", store.getAddress(), contentIntent);
		notificationManager.notify(NOTIFY_ID, notification);
		

	}




android4.0以后:


private static final int NOTIFY_ID = 0;
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
private void showNotification(Store store) {
		Intent intent = new Intent(this,AlarmActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
		intent.putExtra("store", store);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);//FLAG_ONE_SHOT

		Notification notification = new Notification.Builder(context)         
							.setContentTitle(store.getStoreName()+"("+store.getDistance()+")")
							.setContentText(store.getAddress())
							.setContentIntent(contentIntent)
							.setSmallIcon(R.drawable.ic_launch) 
							.setAutoCancel(true)
							.setWhen(System.currentTimeMillis())
							.setDefaults(Notification.DEFAULT_ALL)
							.getNotification();
		notificationManager.notify(NOTIFY_ID, notification);
		
		//stopSelf();
	}




坑爹的Google API上用法是这样的:


Notification noti = new Notification.Builder(mContext)


.setContentTitle("New mail from " + sender.toString())


.setContentText(subject)


.setSmallIcon(R.drawable.new_mail)


.setLargeIcon(aBitmap)


.build();



我找了半天也没找到build()方法!!!



android Notification 的使用




标签:基本,Notification,用法,FLAG,intent,notification,Intent,store
From: https://blog.51cto.com/u_5454003/6174166

相关文章

  • 集合的基本概念
    一、集合的概念1、集合和元素的概念康托尔定义:人们无意中或思想中将一些确定的、彼此完全不同的客体的总和,这些客体叫做集合中的元素。        互不相同的、确定的对象的全体称为集合,简称集。这些对象作为集合的成员,称为集合的元素。常用大写字母表示集合,用小写......
  • mac notification
    displaynotification"message"withtitle"title"subtitle"subtitle"displaynotification"message"soundname"SoundName"Validsoundnamesarethenamesofsoundslocatedin…~/Library/Sounds......
  • JavaScript ES6中class的用法
    实例代码如下classPerson{constructor(name){if(!arguments.length){console.log("我是个人")}else{console.log(`我是${name}`)}}......
  • 学习-ts基本类型
    未完待续.../***基本类型:*为了让程序有价值,我们需要能够处理最简单的数据单元:数字,字符串,结构体*,布尔值等。TypeScript支持与JavaScript几乎相同的数据类型,此外还提供了*实用的枚举类型方便我们使用。*///布尔值letisDone:boolean=true;//数字letdecLite......
  • Vue进阶(四十五):精解 ES6 Promise 用法
    一、前言复杂难懂概念先不讲,我们先简单粗暴地把Promise用一下,有个直观感受。那么第一个问题来了,Promise是什么呢?是类?对象?数组?函数?别猜了,console.dir(Promise)直接打印出来看看。这么一看就明白了,Promise是一个构造函数,自己身上有all、reject、resolve这几个眼熟的方法,原型上有th......
  • 基本下拉菜单
    基本下拉菜单当鼠标移动到指定元素上时,会出现下拉菜单。实例<style>.dropdown{position:relative;display:inline-block;}.dropdown-content{display:none;position:absolute;margin:0px;padding:0px;color:gray;">#f9f9f9;min-width:160px;box-shad......
  • k8s入门篇-Kubernetes的基本概念和术语
    1.k8s基本概念概述Kubernetes中的大部分概念如Node、Pod、ReplicationController、Service等都可以被看作一种资源对象,几乎所有资源对象都可以通过Kubernetes提供的kubectl工具(或者API编程调用)执行增、删、改、查等操作并将其保存在etcd中持久化存储。从这个角度来看,Kubernetes......
  • VsCode开发工具的入门及基本使用
    (VsCode开发工具的入门及基本使用)一、VsCode介绍1.VsCode简介VisualStudioCode(简称“VSCode”)是Microsoft在2015年4月30日Build开发者大会上正式宣布一个运行于MacOSX、Windows和Linux之上的,针对于编写现代Web和云应用的跨平台源代码编辑器,可用于Windows,macOS和Lin......
  • 【c&c++】C语言 char*和char[]用法
    char[]定义的是一个字符数组,注意强调是数组。char*定义的是一个字符串指针,注意强调是指针。char*s定义了一个char型的指针,它只知道所指向的内存单元,并不知道这个内存单元有多大,所以:当char*s=“hello”;后,不能使用s[0]=‘a’;语句进行赋值。这是将提示内存不能为"written"......
  • #error and #line 用法
    目前#error和#line很少被使用,但是也可以作为定位问题原因的工具#error用于生成一个编译错误的信息。用于自定义程序员特有的编译错误信息。在预处理时起作用。#errormessage//message打印的编译error信息,不需要双引号包围#warningmessage//由于可能在编译的过程中......