Skip to content Skip to sidebar Skip to footer

Android Gcm Turn On Lights

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 mobil

Solution 1:

For More Information refer this Link.

Add permission to your manifest file

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

EDIT // 1. Get a reference to the NotificationManager

Stringns= Context.NOTIFICATION_SERVICE;
NotificationManagermNotificationManager= (NotificationManager) getSystemService(ns);

// 2. Instantiate the Notification

int icon = R.drawable.notification_icon;
CharSequence tickerText = "Hello";
longwhen = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);

// 3. Define the Notification's expanded message and Intent

Contextcontext= getApplicationContext();
CharSequencecontentTitle="My notification";
CharSequencecontentText="Hello World!";
IntentnotificationIntent=newIntent(this, MyClass.class);
PendingIntentcontentIntent= PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

// 4. Pass the Notification to the NotificationManager

privatestaticfinalint HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);

// ---------------------- // Add Sound // ---------------------- // a. Default sound

notification.defaults |= Notification.DEFAULT_SOUND;

// 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. Custom vibration

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

// ------------------------ // Add Flashing Lights // ------------------------ // a. Default lights

notification.defaults |= Notification.DEFAULT_LIGHTS;

// b. Custom lights

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

Solution 2:

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


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


    IntentnotificationIntent=newIntent(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);
    PendingIntentintent= PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);


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

Solution 3:

please Add this code in set Notification method

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

Solution 4:

use this

notification.flags |= Notification.FLAG_SHOW_LIGHTS;

refer this link

Post a Comment for "Android Gcm Turn On Lights"