要做到這一點,你將不得不修改您的應用程序。 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閃爍並播放手機的默認通知聲音,「根據每部手機的不同而不同」。
我希望這有助於回答你的問題。
你想添加自己的自定義聲音嗎? – 2015-01-21 02:24:17
我想使用默認通知聲音,這是從移動到另一個不同。就像本機應用程序一樣。 – Eldeeb 2015-01-21 14:29:54
檢查我發佈的答案,並讓我知道它是否有幫助。 – 2015-01-22 19:54:53