【问题标题】:Default push notification sound in Worklight 6.1Worklight 6.1 中的默认推送通知声音
【发布时间】:2015-01-20 21:37:06
【问题描述】:

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

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

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

我也尝试分配像 notification.GCM.sound = "true"notification.GCM.sound = "default" 这样的值,但它在某些设备上播放连续声音。

【问题讨论】:

  • 您要添加自己的自定义声音吗?
  • 我想使用与移动设备不同的默认通知声音。就像原生应用所做的一样。
  • 检查我发布的答案,如果有帮助请告诉我。
  • 按预期工作。谢谢

标签: android push-notification ibm-mobilefirst


【解决方案1】:

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

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

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 闪烁并播放您手机的默认通知声音“因每部手机而异”。

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

【讨论】:

    【解决方案2】:

    【讨论】:

    • 我试过它是空的,但它让通知保持沉默。
    • 所以另一种解决方案是下载默认声音文件并将其添加为自定义声音文件。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-01
    • 1970-01-01
    相关资源
    最近更新 更多