Android:推送通知没有声音/振动/光

时间:2016-04-26 14:37:27

标签: android push-notification

收到通知时,我没有声音,振动或光线。有人可以告诉我我错过了什么吗?

我还有其他一些问题:

1。)我只在应用程序打开时获取指定的图标。当应用程序关闭时,我会获得android徽标图标(我想那是因为我还没有定义应用程序图标)。

2。)仅在应用程序打开时显示自动收报机文本。

3。)当应用程序打开时,我没有获取内容文本,只有内容标题。

解决方案:这是因为我使用com.google.android.gms:play-services-gcm:8.4.0而不是之前的版本。

确保在服务器上发送的通知数组只包含键/值对 e = 0 ,同时在数据阵列中发送消息信息。

这个问题在这里已经有了很好的答案:After updating Google play services to 8.4.0 push notifications displayed by themselves

这是我的来源:

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();
    }
}

3 个答案:

答案 0 :(得分:2)

推送通知的自定义声音

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;

答案 1 :(得分:0)

mBuilder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
mBuilder.setLights(getResources().getColor(R.color.mainColor), 1000, 1000);

您可以在构建器对象上设置颜色和振动

答案 2 :(得分:0)

你错过了这样的事情:

 long[] pattern = new long[]{0, 100, 200, 100}; //two short beeps
 mBuilder.setVibrate(pattern);
 Notification note = mBuilder.build();
 note.vibrate = pattern;

那会给你震动。看看灯光,我手边没有那个代码。

相关问题