Android:推送通知没有声音/振动/光 [英] Android: Push Notification has no Sound/Vibration/Light

查看:204
本文介绍了Android:推送通知没有声音/振动/光的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在收到通知时没有听到声音、振动或灯光.有人可以告诉我我错过了什么吗?

I'm not getting sound, vibration or light when receiving a notification. Can someone please tell me what I'm missing?

我还有其他一些问题:

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

1.) I only get the specified icon when the app is open. When the app is closed I get the android logo icon (I guess thats because I havent defined an app icon yet).

2.) 股票代码仅在应用打开时显示.

2.) The ticker text is only shown when the app is open.

3.) 当应用程序打开时,我没有得到内容文本,只有内容标题.

3.) When the app is open I dont get the content text, only the content title.

解决方案:发生这种情况是因为我使用的是 com.google.android.gms:play-services-gcm:8.4.0 而不是以前的版本.

SOLUTION: This happens because I am using com.google.android.gms:play-services-gcm:8.4.0 over the previous version.

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

Make sure that on the server you send a notification array containing only the key/value pair e=0 while sending your message information in the data array.

这个问题在这里已经有了很好的答案:将 Google Play 服务更新为 8.4.0 自己显示的推送通知后

This problem has already a great answer here: 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();
    }
}

推荐答案

推送通知的自定义声音

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;

这篇关于Android:推送通知没有声音/振动/光的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆