简体   繁体   中英

Default push notification sound in Worklight 6.1

I'm using Worklight Push Notification but on Android the push comes with no sound. I want to enable default sound (and LED if possible).

I'm using the sample push notification example code.

var notification = WL.Server.createDefaultNotification(notificationText, badgeDigit, {custom:"data"});

I also tried to assigning a value like notification.GCM.sound = "true" or notification.GCM.sound = "default" but it is playing continuous sound on some devices.

To accomplish this you will have to modify your app. Worklight will generate a skeleton class in your Android project, GCMIntentService.java

In order to add sound and flash the LED notification light, you will have to override the notify methods in the GCMIntentService class. Your file will look like this:

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;

public class GCMIntentService extends
    com.worklight.androidgap.push.GCMIntentService {
  @Override
  public void notify(Context context, String alert, int badge, String sound,
      Intent intent) {
    super.notify(context, alert, badge, sound, intent);

    // call helper method
    notifyLightAndSound(context);
  }

  @Override
  public void notify(Context context, String tickerText) {
    super.notify(context, tickerText);

    // call helper method
    notifyLightAndSound(context);

  }

  private void notifyLightAndSound(Context context) {

    // Get the default notification sound
    Uri notification = RingtoneManager
        .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    // build a notification with the light and sound
    // LED will be on for 1000 ms and off for 800 ms until you turn on your
    // screen
    Notification n = new Notification.Builder(context)
        .setLights(Notification.DEFAULT_LIGHTS, 1000, 800)
        .setSound(notification).build();

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // play sound and flash LED
    mNotificationManager.notify(4, n);

  }
}

This will flash the LED and play the default notification sound of your phone "different based on each phone".

I hope this helps to answer your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM