简体   繁体   中英

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

I'm using FCM to make a push message.

I implemented all the push mechanism by copying and pasting the codes on FCM website.

After doing this, I have been testing several times to check if it works well. I checked with Log.d and I found that both onMessageReceived and sendPushNotification methods are triggered when a push message is received. However, when the message is received, my phone doesn't make any sound, no vibration, no head up.

The message is just displayed on the screen when I turn the screen on.

Can anyone let me know how to fix these problems?

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

您发送的通知有效负载是多少,如果您的应用程序处于后台,根据FCM文档,它应该有一个sound密钥,只有当您的应用程序位于Foreground时,您的代码才有效。

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