此处将为大家介绍关于有没有办法Jest测试和监视WebNotificationAPI?的详细内容,并且为您解答有关testjnz的相关问题,此外,我们还将为您介绍关于androidNotificati
此处将为大家介绍关于有没有办法 Jest 测试和监视 Web Notification API?的详细内容,并且为您解答有关test jnz的相关问题,此外,我们还将为您介绍关于android Notification 的一个简单应用(在 Notification 中嵌入一个进度条,并且这个 Notification 点击消失但不会跳转)、Android Notification 详解 —— 响应 notification 事件、android notification,notificationmanager详解、android – 我们能否以编程方式对Notification Listener Service的Notification执行ACTION?的有用信息。
本文目录一览:- 有没有办法 Jest 测试和监视 Web Notification API?(test jnz)
- android Notification 的一个简单应用(在 Notification 中嵌入一个进度条,并且这个 Notification 点击消失但不会跳转)
- Android Notification 详解 —— 响应 notification 事件
- android notification,notificationmanager详解
- android – 我们能否以编程方式对Notification Listener Service的Notification执行ACTION?
有没有办法 Jest 测试和监视 Web Notification API?(test jnz)
您应该模拟 Notification
类及其静态成员。
Notification.permission 是静态属性,Notification.requestPermission() 是静态方法。
例如
describe('66631725',() => {
const mNotification = jest.fn();
Object.defineProperty(global,'Notification',{
value: mNotification,});
const staticMembers = {
requestPermission: jest.fn().mockImplementation(() => {
console.log('reached here');
return 'denied';
}),permission: 'denied',};
Object.assign(global.Notification,staticMembers);
test('should ask for permission from Notifications API ',() => {
new Notification('Test');
expect(Notification).toBeCalledTimes(1);
expect(Notification.permission).toEqual('denied');
});
test('should request permission',() => {
Notification.requestPermission();
expect(Notification.requestPermission).toBeCalledTimes(1);
});
});
测试结果:
PASS examples/66631725/index.test.ts
66631725
✓ should ask for permission from Notifications API (3 ms)
✓ should request permission (13 ms)
console.log
reached here
at Function.<anonymous> (examples/66631725/index.test.ts:9:15)
Test Suites: 1 passed,1 total
Tests: 2 passed,2 total
Snapshots: 0 total
Time: 4.284 s
android Notification 的一个简单应用(在 Notification 中嵌入一个进度条,并且这个 Notification 点击消失但不会跳转)
网上很多的例子都是直接获取 Notification 对象来设置一个通知,其实 Notification 跟 Dialog 一样,也有自己的 Builder,可以用 builder 对象来设置一个 Notification
这个例子是在 Notification 中嵌入一个进度条,并且这个 Notification 点击消失但不会跳转(跟 android 的 vcard 文件导入时弹出的 Notification 一样)
NotificationManager mNotificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(context);
builder.setOngoing(true);
builder.setProgress (total, current, false);// 设置进度条,false 表示是进度条,true 表示是个走马灯
builder.setTicker (title);// 设置 title
builder.setWhen(System.currentTimeMillis());
builder.setContentTitle (content);// 设置内容
builder.setAutoCancel (true);// 点击消失
builder.setSmallIcon(R.drawable.upload);
builder.setContentIntent (PendingIntent.getActivity (context, 0, new Intent (), 0));// 这句和点击消失那句是 “Notification 点击消失但不会跳转” 的必须条件,如果只有点击消失那句,这个功能是不能实现的
Notification noti = builder.getNotification();
mNotificationManager.notify(id,noti);
希望这个例子对其他人有点用,因为我特曾为这个功能苦恼过,呵呵!
Android Notification 详解 —— 响应 notification 事件
上一篇讲了如何创建并显示一个 notification,这一篇就总结下点击 notification 后,程序应该如何响应。
一般来讲,点击一个 notification 后,都会打开一个 Activity 做为对点击事件的响应,这个 Activity 是之前在 PendingIntent 中设置好的。
经常玩 Android 手机的应该都有印象,在日历应用中,你新建一个提醒,当提醒通知收到后,你点击通知,会进入提醒的内容页面,如果这个时候按 back 键,会直接退出应用。
但是在 Gmail 的应用中,如果有一封新邮件到来,那么点击通知后,会进入到邮件的内容页面,等你看完邮件,点击 back 键,会退到邮件列表页面,再按 back 键,才会退出应用。
我们总结一下两种情况,假设我们的应用有两个 Activity(ParentActivity、SubActivity),notification 中设置打开的 Activity 为 SubActivity。
那么第一种情况就是:
点击 Notification ——> 进入 SubActivity ——> back 键 ——> 退出应用
第二种情况:
点击 Notification ——> 进入 SubActivity ——> back 键 ——> 退到 ParentActivity ——>back 键 ——> 退出应用
第一种情况比较简单,只需要在 PendingIntent 中指定 Activity,不需要其他设置,Android 默认的就这样。
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
但是在创建 PendingIntent 的时候需要注意参数 PendingIntent.FLAG_CANCEL_CURRENT
这个标志位用来指示:如果当前的 Activity 和 PendingIntent 中设置的 intent 一样,那么久先取消当前的 Activity,用 PendingIntent 中指定的 Activity 取代之。
另外,需要在 Manifest 中对指定的 Activity 设置属性
<activity android:name=".SubActivityl"
android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true">
</activity>
第二种情况稍微复杂点,因为如果只打开一个 SubActivity,程序并没办法知道他的上一级 Activity 是谁,所以需要在点击 Notification 时打开一组 Activity,但是我们并不需要一个个去调用 startActivity 方法,PendingIntent 提供了个静态方法 getActivities,里面可以设置一个 Intent 数组,用来指定一系列的 Activity。
所以我们首先写一个函数创建一个 Activity 数组:
Intent[] makeIntentStack(Context context) {
Intent[] intents = new Intent[2];
intents[0] = Intent.makeRestartActivityTask(new ComponentName(context, com.example.notificationtest.MainActivity.class));
intents[1] = new Intent(context, com.example.notificationtest.SubActivity.class);
return intents;
}
其中需要注意的是 Intent.makeRestartActivityTask 方法,这个方法用来创建 activity 栈的根 activity
接下来,创建并显示 Notification:
void showNotification(Intent intent) {
Notification notification = new Notification(
R.drawable.status_icon,
"Hello World ticker text",
System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivities(
this,
0,
makeIntentStack(this),
PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(
this,
"Title",
"Hey, shall we have a dinner tonight",
contentIntent);
notification.flags |= Notification.DEFAULT_ALL;
mNM.notify(1, notification);
}
android notification,notificationmanager详解
我们知道在使用Android的通知的时候一定会用到NotificationManager 、 Notification这两个类,这两个类的作用分别是:
NotificationManager : 是状态栏通知的管理类,负责发通知、清楚通知等。
Notification:状态栏通知对象,可以设置icon、文字、提示声音、振动等等参数。
这里需要声明一点,由于Android的系统升级,Android在通知这块也有很多老的东西被抛弃了,一个是api11的版本,一个是api16的版本。我们来比较下api11之前的用法这是通用的:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
// 下面需兼容Android 2.x版本是的处理方式
Notification notify1 = new Notification();
notify1.icon = R.drawable.message;
notify1.tickerText = "TickerText:您有新短消息,请注意查收!";
notify1.when = System.currentTimeMillis();
notify1.setLatestEventInfo(this, "Notification Title",
"This is the notification message", pendingIntent);
notify1.number = 1;
notify1.flags |= Notification.FLAG_AUTO_CANCEL;
manager.notify(NOTIFICATION_FLAG, notify1);
api11-api16的用法是这样的(主要是新增了自定义通知图标,并且通知的构造方式也发生了改变)
PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
// API11之后才支持
Notification notify2 = new Notification.Builder(this)
.setSmallIcon(R.drawable.message)
.setTicker("TickerText:" + "您有新短消息,请注意查收!")
.setContentTitle("Notification Title")
.setContentText("This is the notification message")
.setContentIntent(pendingIntent2)
.setNumber(1)
.getNotification(); // 需要注意build()是在API level
// 16及之后增加的,在API11中可以使用getNotificatin()来代替
notify2.flags |= Notification.FLAG_AUTO_CANCEL;
manager.notify(NOTIFICATION_FLAG, notify2);
api16之后
PendingIntent pendingIntent3 = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
// API16之后才支持
Notification notify3 = new Notification.Builder(this)
.setSmallIcon(R.drawable.message)
.setTicker("TickerText:" + "您有新短消息,请注意查收!")
.setContentTitle("Notification Title")
.setContentText("This is the notification message")
.setContentIntent(pendingIntent3).setNumber(1).build();
notify3.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。
manager.notify(NOTIFICATION_FLAG, notify3);//关联通知
我们这里讲的主要是api16之后的使用方法
首先我们通过系统的Service获取NotificationManager对象,然后通过他将消息发送给系统,获取方法如下:
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification主要包含以下参数:
- An icon (通知的图标)
- A title and expanded message (通知的标题和内容)
- A
PendingIntent (点击通知执行页面跳转)
1、创建NotificationManager
通过NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);获取NotificationNotificationManager 消息管理类,
2,创建Notification实体
通过Notification.Builder builder = new Notification.Builder(this);创建一个通知的实体,里面可以包含很多的参数,如通知的Icon,消息内容,跳转等。
3,通过notificationManager.notify(0, builder.build());将消息绑定,里面会用到NotificationService(这里不做讲解)
普通通知
Notification.Builder builder = new Notification.Builder(this);
Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.csdn.net/xiangzhihong8"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);
builder.setContentIntent(pendingIntent);
builder.setSmallIcon(R.drawable.lanucher);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.lanucher));
builder.setAutoCancel(true);
builder.setContentTitle("普通通知");
builder.setContentText("您有新短消息,请注意查收");
notificationManager.notify(0, builder.build());
折叠式通知
我们还可以通过RemoteViews(这里就是桌面小控件的实现,不知道大家是否还有印象)
Notification.Builder builder = new Notification.Builder(this);
Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.csdn.net/itachi85/"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);
builder.setContentIntent(pendingIntent);
builder.setSmallIcon(R.drawable.foldleft);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.lanucher));
builder.setAutoCancel(true);
builder.setContentTitle("折叠菜单");
builder.setContentText("您有新短消息,请注意查收");
//用RemoteViews来创建自定义Notification视图
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.view_fold);
Notification notification = builder.build();
//指定展开时的视图
notification.bigContentView = remoteViews;
notificationManager.notify(1, notification);
自定义通知
Notification.Builder builder = new Notification.Builder(this);
Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.csdn.net/xiangzhihong8"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);
builder.setContentIntent(pendingIntent);
builder.setSmallIcon(R.drawable.foldleft);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon));
builder.setAutoCancel(true);
builder.setContentTitle("自定义菜单");
builder.setContentText("您有新短消息,请注意查收");
//设置点击跳转
Intent hangIntent = new Intent(this,NotificationActivity.class);
hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent hangPendingIntent = PendingIntent.getActivity(this, 0, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setFullScreenIntent(hangPendingIntent, true);
notificationManager.notify(2, builder.build());


源码:
http://download.csdn.net/detail/xiangzhihong8/9639345
本文同步分享在 博客“xiangzhihong8”(CSDN)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。
android – 我们能否以编程方式对Notification Listener Service的Notification执行ACTION?
是否可以以编程方式从NotificationListenerService的Notification上调用ACTION?
我编写了一个Notification Listener服务,它在完成后读出所有传入的通知和解雇.但我不确定我们是否可以对传入的通知执行任何操作.
例如:在WhatsApp消息通知中,我可以读取包名称,发件人和邮件详细信息,但有没有办法将回复发送回发件人?
目前Android Wear正在这样做,所以我想知道它是否采用了一种通用方法来处理通知,或者它是否具有针对WhatsApp服务的特定API.
解决方法:
当通过通知listerner服务收到whatsapp通知时,还有另一个收到(隐藏)的通知,其标签值类似于这个XXXXXXX@s.whatsapp.net(xxxxxxx是phonenumber),我认为这在某种程度上持有密钥发送回复给这个用户.
我正在研究其他应用程序逻辑,它不需要你发回回复但我发现这很有趣,但没有足够的时间来检查它自己.
我们今天的关于有没有办法 Jest 测试和监视 Web Notification API?和test jnz的分享已经告一段落,感谢您的关注,如果您想了解更多关于android Notification 的一个简单应用(在 Notification 中嵌入一个进度条,并且这个 Notification 点击消失但不会跳转)、Android Notification 详解 —— 响应 notification 事件、android notification,notificationmanager详解、android – 我们能否以编程方式对Notification Listener Service的Notification执行ACTION?的相关信息,请在本站查询。
本文标签: