Android GCM 开灯 [英] Android GCM turn on Lights
问题描述
我正在 Android 中做一个项目.我可以成功接收推送通知.
I'm doing a project in Android. I can successfully receive push notifications.
收到推送通知后如何开灯?
How to turn on the lights when I receive the push notification?
而且我还需要在收到推送通知时振动我的手机.
And also I need to vibrate my mobile when receiving the push notification.
推荐答案
有关更多信息,请参阅此 链接.
For More Information refer this Link.
为您的清单文件添加权限
Add permission to your manifest file
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
编辑//1. 获取对 NotificationManager 的引用
EDIT // 1. Get a reference to the NotificationManager
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
//2. 实例化通知
int icon = R.drawable.notification_icon;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
//3.定义Notification的扩展信息和Intent
// 3. Define the Notification's expanded message and 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);
//4. 将通知传递给 NotificationManager
// 4. Pass the Notification to the NotificationManager
private static final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
//---------------//添加声音//----------------------//一个.默认声音
// ---------------------- // Add Sound // ---------------------- // a. Default sound
notification.defaults |= Notification.DEFAULT_SOUND;
//B.来自 SD 卡的自定义声音
// b. Custom sound from SD card
notification.sound = Uri.parse("file:///sdcard/notification/SOUND.mp3");
//---------------//添加振动//----------------------//一个.默认振动
// ---------------------- // Add Vibration // ---------------------- // a. Default vibration
notification.defaults |= Notification.DEFAULT_VIBRATE;
//B.自定义振动
long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;
//------------------------//添加闪光灯//------------------------//一个.默认灯
// ------------------------ // Add Flashing Lights // ------------------------ // a. Default lights
notification.defaults |= Notification.DEFAULT_LIGHTS;
//B.自定义灯
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
这篇关于Android GCM 开灯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!