Android: Push Notification has no Sound/Vibration/

2019-05-28 13:49发布

问题:

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

Also I have some other questions:

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.) The ticker text is only shown when the app is open.

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

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

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.

This problem has already a great answer here: After updating Google play services to 8.4.0 push notifications displayed by themselves

This is my Source:

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

回答1:

Custom Sound for Push Notification

notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.pop);

In your code change

    Notification notification = mBuilder.build();
    notification.defaults = Notification.DEFAULT_ALL;
    mNotifyMgr.notify(mNotificationId, notification);

To.

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

you can set color and vibrate on the builder object



回答3:

You're missing something like this:

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

That will give you vibrations. Look into lights, I don't have that code at hand atm.