[英]Android: Push Notification has no Sound/Vibration/Light
收到通知時,我沒有聲音,振動或光線。 有人可以告訴我我錯過了什么嗎?
我還有一些其他問題:
1.)我只在應用程序打開時獲取指定的圖標。 當應用程序關閉時,我得到了android徽標圖標(我想那是因為我還沒有定義應用程序圖標)。
2.)僅在應用程序打開時顯示自動收報機文本。
3.)當應用程序打開時,我沒有得到內容文本,只有內容標題。
解決方案:這是因為我使用com.google.android.gms:play-services-gcm:8.4.0
而不是之前的版本。
確保在服務器上發送的通知數組只包含鍵/值對e = 0,同時在數據數組中發送消息信息。
這個問題在這里已經有了一個很好的答案: 將Google Play服務更新為自己顯示的8.4.0推送通知
這是我的來源:
public class MyGcmListenerService extends GcmListenerService {
private final static String TAG = "GCM_Listener";
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
Log.d(TAG, "From: " + from + " (" + message);
// Sets an ID for the notification
SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.sharedPreferenceStore_default), Context.MODE_PRIVATE);
int mNotificationId = sharedPreferences.getInt("id_notification", 0);
mNotificationId++;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.all_picks_made_indicator)
.setAutoCancel(true)
.setContentTitle("Product - " + mNotificationId)
.setContentText(message)
.setTicker("Product Notification received");
// Because clicking the notification opens a new ("special") activity, there's no need to create an artificial back stack.
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, DetectLoginActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
Notification notification = mBuilder.build();
notification.defaults = Notification.DEFAULT_ALL;
mNotifyMgr.notify(mNotificationId, notification);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("id_notification", mNotificationId);
editor.commit();
}
}
推送通知的自定義聲音
notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.pop);
在你的代碼更改
Notification notification = mBuilder.build();
notification.defaults = Notification.DEFAULT_ALL;
mNotifyMgr.notify(mNotificationId, notification);
至。
notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" +R.raw.pop);
notification.defaults |= Notification.DEFAULT_VIBRATE;
mBuilder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
mBuilder.setLights(getResources().getColor(R.color.mainColor), 1000, 1000);
您可以在構建器對象上設置顏色和振動
你錯過了這樣的事情:
long[] pattern = new long[]{0, 100, 200, 100}; //two short beeps
mBuilder.setVibrate(pattern);
Notification note = mBuilder.build();
note.vibrate = pattern;
這會給你振動。 看看燈光,我手頭沒有那個代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.