No notification sound when sending notification from firebase in android

前端 未结 9 1723
迷失自我
迷失自我 2020-12-01 01:00

I am sending push notification from firebase to my Android Application. but when my app is in background firebase onMessageReceived method is not called instead firebase sen

9条回答
  •  心在旅途
    2020-12-01 01:48

    try this....

      public  void buildPushNotification(Context ctx, String content, int icon, CharSequence text, boolean silent) {
        Intent intent = new Intent(ctx, Activity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 1410, intent, PendingIntent.FLAG_ONE_SHOT);
    
        Bitmap bm = BitmapFactory.decodeResource(ctx.getResources(), //large drawable);
    
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(ctx)
                .setSmallIcon(icon)
                .setLargeIcon(bm)
                .setContentTitle(content)
                .setContentText(text)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);
    
        if(!silent)
           notificationBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
    
     NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
    
            notificationManager.notify(1410, notificationBuilder.build());
        }
    

    and in onMessageReceived, call it

     @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
    
    
        Log.d("Msg", "Message received [" + remoteMessage.getNotification().getBody() + "]");
    
        buildPushNotification(/*your param*/);
    }
    

    or follow KongJing, Is also correct as he says, but you can use a Firebase Console.

提交回复
热议问题