安卓:推送通知没有声音/震动/闪光灯

3

我收到通知时没有声音、振动或闪光灯。请问有人能告诉我错过了什么吗?

此外,我还有一些其他问题:

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个回答

3

推送通知的自定义声音

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;

当我关闭一个应用程序后,就会收到没有震动的推送通知。在其他情况下,通知会有震动。有人知道原因吗? - nAkhmedov

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

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


1
类似的方法用于声音。 - kimchibooty
我已经设置 notification.defaults = Notification.DEFAULT_ALL; - Markus

0
你缺少了类似这样的东西:
 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;。我不明白为什么要先在构建器类中指定振动,然后再通过公共变量重新设置它的意义所在。 - Markus
请先阅读文档,再试图用那种论点说服我放弃可行的解决方案,因为它们并不包含在DEFAULT_ALL选项中。 - Shark
文档(http://developer.android.com/reference/android/app/Notification.html)指出DEFAULT_ALL将使用默认的震动、灯光和声音。 - Markus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接