繁体   English   中英

Worklight 6.1中的默认推送通知声音

[英]Default push notification sound in Worklight 6.1

我正在使用Worklight推送通知,但在Android上,推送没有声音。 我想启用默认声音(如果可能,请启用LED)。

我正在使用示例推送通知示例代码。

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

我还尝试分配一个值,例如notification.GCM.sound = "true"notification.GCM.sound = "default"但它在某些设备上正在播放连续的声音。

为此,您必须修改您的应用程序。 Worklight将在您的Android项目GCMIntentService.java中生成一个骨架类。

为了添加声音并使LED通知灯闪烁,您将必须重写GCMIntentService类中的notify方法。 您的文件将如下所示:

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

  }
}

这将使LED闪烁并播放电话的默认通知声音“根据每个电话而有所不同”。

我希望这有助于回答您的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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