2015-01-20 82 views
0

我正在使用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

回答

1

要做到這一點,你將不得不修改您的應用程序。 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
+0

我試過了,但它保持通知無聲。 – Eldeeb 2015-01-21 13:41:22

+0

因此,另一種解決方案是下載默認聲音文件並將其添加爲自定義聲音文件。 :) – 2015-01-21 13:46:48