Android GCM turn on Lights

前端 未结 4 790
南方客
南方客 2020-12-15 13:45

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

4条回答
  •  一生所求
    2020-12-15 14:01

        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());
    

提交回复
热议问题