Android 安卓GCM开灯

Android 安卓GCM开灯,android,push-notification,google-cloud-messaging,Android,Push Notification,Google Cloud Messaging,我正在用Android做一个项目。我可以成功接收推送通知 当我收到推送通知时,如何打开灯 当收到推送通知时,我还需要振动手机。请在设置通知方法中添加此代码 notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.defaults |= Notification.DEFAULT_SOUND; notification.defaults |= Notification.DEFAULT_L

我正在用Android做一个项目。我可以成功接收推送通知

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


当收到推送通知时,我还需要振动手机。

请在设置通知方法中添加此代码

         notification.flags   |= Notification.FLAG_AUTO_CANCEL;
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.defaults |= Notification.DEFAULT_LIGHTS;
    notification.defaults |= Notification.DEFAULT_VIBRATE;

有关更多信息,请参阅此

向清单文件添加权限

<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
//二,。实例化通知

int icon = R.drawable.notification_icon;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
//三,。定义通知的扩展消息和意图

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);
//四,。将通知传递给NotificationManager

String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
private static final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
//---------------------- //添加声音 // ---------------------- //a。默认声音

notification.defaults |= Notification.DEFAULT_SOUND;
//b。来自SD卡的自定义声音

notification.sound = Uri.parse("file:///sdcard/notification/SOUND.mp3");
//---------------------- //增加振动 // ---------------------- //a。缺省振动

notification.defaults |= Notification.DEFAULT_VIBRATE;
long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;
//b。定制振动

notification.defaults |= Notification.DEFAULT_VIBRATE;
long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;
//------------------------ //添加闪光灯 // ------------------------ //a。默认灯光

notification.defaults |= Notification.DEFAULT_LIGHTS;
//b。定制灯光

notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
用这个

notification.flags |= Notification.FLAG_SHOW_LIGHTS;
参考此