简体   繁体   中英

Android Notification Not getting Sound And Vibration

I've used FCM Notifications in My app , I'm Receiving Them and it's showing Title And message Perfectly

But,When i'm receiving the Notification Im not getting any sound or vibration or any Led light indication

My Notification Builder Code is

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    builder.setSmallIcon(R.drawable.applogo);
    builder.setContentTitle(Title);
    builder.setContentText(Message);
    builder.setAutoCancel(true);
    builder.setSound(Uri.parse("file:///android_asset/notification.mp3"));
    builder.setContentIntent(pendingIntent);
    builder.setDefaults(Notification.DEFAULT_VIBRATE);
    builder.setLights(Color.RED, 1000, 300);

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

    notificationManager.notify(0, builder.build());

Thanks in Advance.

1. When you send push notification from firebase make sure that sound option enabled

like this 在此处输入图片说明

2. Also make sure that you have added permissions in android manifest for vibrate.

<uses-permission android:name="android.permission.VIBRATE" />

3. If you are sending from server than use payload

notification payload of the notification there is a sound key.

From the official documentation its use is:

Indicates a sound to play when the device receives a notification. Supports default or the filename of a sound resource bundled in the app. Sound files must reside in /res/raw/.

{
    "to" : ".......",

    "notification" : {
      "body" : "body",
      "title" : "title",
      "icon" : "myicon",
      "sound" : "default"
    }
  }

Try this

Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);

IMO,您必须在清单文件中添加权限,如下所示

<uses-permission android:name="android.permission.VIBRATE" />

Android Notification Not getting Sound And Vibration

Try to use default RingtoneManager

Remove builder.setSound(Uri.parse("file:///android_asset/notification.mp3"));

Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(uri);

Added permissions in android manifest for vibrate.

<uses-permission android:name="android.permission.VIBRATE" />

Important Note: Make sure Android Push Notification Service correctly configure by cloud server-side(GCM/Firebase)

Confirm that you are getting correct payload for downstream (JSON)

 {
      "to": "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
      "notification": {
        "body": "great match!",
        "title": "SamDev Test",
        "icon": "myicon",
        "sound": "default"
      }
    }

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