简体   繁体   中英

android GCM push, no vibration, no sound, no head up

i'm using GCM to make a notification.

i ask the app server to send the push notification to an user, and the server does it.

when the push notification is received, the message is displayed on the screen well. but it doesn't make any sound or vibration, of course no head up.

so, here's my code of course, i already added two uses-permission(WAKE_LOCK, VIBRATE)

public class FirebaseMessagingService  extends com.google.firebase.messaging.FirebaseMessagingService {
    private static final String TAG = "FirebaseMsgService";

    // [START receive_message]
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

       // sendPushNotification(remoteMessage.getData().get("message"));



        if(remoteMessage.getData().size() > 0){

            sendPushNotification(remoteMessage.getNotification().getBody());

        }

    }

    private void sendPushNotification(String message) {
        System.out.println("received message : " + message);

        Intent intent = new Intent(this, Activity_Login_Main.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0  //Request code
                , intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);  
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.backicon).setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher) )
                .setContentTitle("Push Title ")
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setDefaults(Notification.DEFAULT_SOUND)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wakelock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
        wakelock.acquire(5000);

        notificationManager.notify(0 // ID of notification
    , notificationBuilder.build());
    }
}

I have been in to a similar situation when implementing Firebase Notifications. The problem is when you send a push notification from firebase console without data part, it is received as a Notification message . You should try sending a Data message to trigger onMessageReceived()

See this for details : https://firebase.google.com/docs/cloud-messaging/concept-options

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM