Ama*_*ath 6 android push-notification google-cloud-messaging
我在Android上做一个项目.我可以成功接收推送通知.
我收到推送通知时如何打开灯?
而且我还需要在收到推送通知时振动我的手机.
有关详细信息,请参阅此链接.
添加清单文件的权限
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
Run Code Online (Sandbox Code Playgroud)
编辑 // 1.获取NotificationManager的引用
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
Run Code Online (Sandbox Code Playgroud)
// 2.实例化通知
int icon = R.drawable.notification_icon;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Run Code Online (Sandbox Code Playgroud)
// 3.定义Notification的扩展消息和Intent
Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
Run Code Online (Sandbox Code Playgroud)
// 4.将通知传递给NotificationManager
private static final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
Run Code Online (Sandbox Code Playgroud)
// ---------------------- //添加声音// -------------------- - // 一个.默认声音
notification.defaults |= Notification.DEFAULT_SOUND;
Run Code Online (Sandbox Code Playgroud)
// b.来自SD卡的自定义声音
notification.sound = Uri.parse("file:///sdcard/notification/SOUND.mp3");
Run Code Online (Sandbox Code Playgroud)
// ---------------------- //添加振动// -------------------- - // 一个.默认振动
notification.defaults |= Notification.DEFAULT_VIBRATE;
Run Code Online (Sandbox Code Playgroud)
// b.定制振动
long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;
Run Code Online (Sandbox Code Playgroud)
// ------------------------ //添加闪烁灯// ----------------- - - - - // 一个.默认灯光
notification.defaults |= Notification.DEFAULT_LIGHTS;
Run Code Online (Sandbox Code Playgroud)
// b.定制灯
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
Run Code Online (Sandbox Code Playgroud)
请在设置通知方法中添加此代码
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.defaults |= Notification.DEFAULT_VIBRATE;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5672 次 |
| 最近记录: |