Android GCM打开Lights

前端之家收集整理的这篇文章主要介绍了Android GCM打开Lights前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在 Android上做一个项目.我可以成功接收推送通知.

我收到推送通知时如何打开灯?

而且我还需要在收到推送通知时振动我的手机.

解决方法

有关更多信息,请参阅此 Link.

添加清单文件的权限

  1. <uses-permission android:name="android.permission.VIBRATE"></uses-permission>

编辑
// 1.获取NotificationManager的引用

  1. String ns = Context.NOTIFICATION_SERVICE;
  2. NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

// 2.实例化通知

  1. int icon = R.drawable.notification_icon;
  2. CharSequence tickerText = "Hello";
  3. long when = System.currentTimeMillis();
  4. Notification notification = new Notification(icon,tickerText,when);

// 3.定义Notification的扩展消息和Intent

  1. Context context = getApplicationContext();
  2. CharSequence contentTitle = "My notification";
  3. CharSequence contentText = "Hello World!";
  4. Intent notificationIntent = new Intent(this,MyClass.class);
  5. PendingIntent contentIntent = PendingIntent.getActivity(this,notificationIntent,0);
  6. notification.setLatestEventInfo(context,contentTitle,contentText,contentIntent);

// 4.将通知传递给NotificationManager

  1. private static final int HELLO_ID = 1;
  2. mNotificationManager.notify(HELLO_ID,notification);

// ———————-
//添加声音
// ———————-
// 一个.默认声音

  1. notification.defaults |= Notification.DEFAULT_SOUND;

// b.来自SD卡的自定义声音

  1. notification.sound = Uri.parse("file:///sdcard/notification/SOUND.mp3");

// ———————-
//添加振动
// ———————-
// 一个.默认振动

  1. notification.defaults |= Notification.DEFAULT_VIBRATE;

// b.定制振动

  1. long[] vibrate = {0,100,200,300};
  2. notification.vibrate = vibrate;

// ————————
//添加闪烁灯
// ————————
// 一个.默认灯光

  1. notification.defaults |= Notification.DEFAULT_LIGHTS;

// b.定制灯

  1. notification.ledARGB = 0xff00ff00;
  2. notification.ledOnMS = 300;
  3. notification.ledOffMS = 1000;
  4. notification.flags |= Notification.FLAG_SHOW_LIGHTS;

猜你在找的Android相关文章