Android的GCM开灯(Android GCM turn on Lights)

2019-07-17 22:01发布

我在做Android中的一个项目。 我可以成功地接收推送通知。

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

而且我需要接收推送通知时振动我的手机。

Answer 1:

更多信息请参考此链接 。

添加权限您的清单文件

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

编辑 // 1.获取对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.定义通知的扩展消息和意图

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

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

// // ----------------------添加声音// -------------------- - // 一个。 默认的声音

notification.defaults |= Notification.DEFAULT_SOUND;

// B。 从SD卡自定义音效

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

// // ----------------------振动添加// -------------------- - // 一个。 默认振动

notification.defaults |= Notification.DEFAULT_VIBRATE;

// B。 自定义振动

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

// ------------------------ //添加闪灯// ----------------- - - - - // 一个。 默认灯光

notification.defaults |= Notification.DEFAULT_LIGHTS;

// B。 自定义灯

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


Answer 2:

    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);


    NotificationCompat.Builder mBuilder     = new NotificationCompat.Builder(context)
                                                    .setSmallIcon(icon)
                                                    .setContentTitle(title)
                                                    .setContentText(message)
                                                    .setAutoCancel(true)
                                                    .setDefaults(Notification.DEFAULT_LIGHTS);


    Intent              notificationIntent  = new Intent(context, MyActivity.class);



    /* Set intent so it does not start a new activity */
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);


    AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);

    /* Even if the mode is set to "Sound & Vibration" in the phone, 
     * the status code that getRingerMode() returns is RINGER_MODE_NORMAL.
     */
    switch (am.getRingerMode()) 
    {
        case AudioManager.RINGER_MODE_VIBRATE:
            mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
            break;
        case AudioManager.RINGER_MODE_NORMAL:
            mBuilder.setDefaults(Notification.DEFAULT_SOUND);
            break;
        default:
            mBuilder.setDefaults(Notification.DEFAULT_SOUND);
     }


    mBuilder.setContentIntent(intent);        
    notificationManager.notify(id, mBuilder.build());


Answer 3:

请在设置通知的方法添加以下代码

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


Answer 4:

用这个

notification.flags |= Notification.FLAG_SHOW_LIGHTS;

请参阅此链接



文章来源: Android GCM turn on Lights