【问题标题】:Android: Push Notification has no Sound/Vibration/LightAndroid:推送通知没有声音/振动/灯光
【发布时间】:2016-04-26 14:37:27
【问题描述】:

收到通知时,我没有听到声音、振动或灯光。有人可以告诉我我缺少什么吗?

我还有一些其他问题:

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

【问题讨论】:

    标签: android push-notification


    【解决方案1】:

    推送通知的自定义声音

    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;
    

    【讨论】:

    • 当我杀死一个应用程序,然后收到推送通知没有振动。在其他情况通知有一个振动。谁知道原因?
    【解决方案2】:
    mBuilder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
    mBuilder.setLights(getResources().getColor(R.color.mainColor), 1000, 1000);
    

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

    【讨论】:

    • 类似的声音方法
    • 我有 notification.defaults = Notification.DEFAULT_ALL;已经
    【解决方案3】:

    你错过了这样的东西:

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

    这会给你带来振动。看看灯,我手头没有那个代码。

    【讨论】:

    • 我有 notification.defaults = Notification.DEFAULT_ALL;已经。我没有看到首先在构建器类中指定振动然后通过公共变量再次设置它的意义
    • 同样,它们不包含在 DEFAULT_ALL 选项中,请在尝试用该论点说服我退出可行的解决方案之前阅读文档。
    • 文档 (developer.android.com/reference/android/app/Notification.html) 声明 DEFAULT_ALL 将使用默认的振动、灯光和声音。
    猜你喜欢
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多