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

问题描述:

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

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

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

我也试着像分配或notification.GCM.sound = "true"一个notification.GCM.sound = "default"值,但它是打连续的声音在某些设备上。

+0

你想添加自己的自定义声音吗? – 2015-01-21 02:24:17

+0

我想使用默认通知声音,这是从移动到另一个不同。就像本机应用程序一样。 – Eldeeb 2015-01-21 14:29:54

+0

检查我发布的答案,并让我知道它是否有帮助。 – 2015-01-22 19:54:53

要做到这一点,你将不得不修改您的应用程序。 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闪烁并播放手机的默认通知声音,“根据每部手机的不同而不同”。

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

+0

我试过了,但它保持通知无声。 – Eldeeb 2015-01-21 13:41:22

+0

因此,另一种解决方案是下载默认声音文件并将其添加为自定义声音文件。 :) – 2015-01-21 13:46:48