萤幕锁定时,我想在Firebase onMessage接收方法上播放自订通知音

时间:2019-04-11 06:24:39

标签: android

我想在firebase onMessage接收方法上播放自定义通知音, 自定义声音仅在应用运行时播放,当应用破坏系统通知默认声音播放时,我想在应用破坏或移动锁定的firebase onMessage接收方法时播放自定义通知。

Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
    Uri sound = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.door_bell);

    RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE, sound);

    Notification mNotification = new Notification.Builder(this)
            .setContentTitle("New Post!")
            .setContentText("Here's an awesome update for you!")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(pIntent)
            .addAction(R.mipmap.ic_launcher, "View", pIntent)
            .addAction(0, "Remind", pIntent)
            .setSound(sound)
            .build();

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    if (notificationManager != null) {
        notificationManager.notify(0, mNotification);
    }
    startActivity(new Intent(getApplicationContext(), MainActivity.class));

1 个答案:

答案 0 :(得分:0)

如果您希望在应用销毁时使用自定义Notification
确保您的Firebase数据具有

之类的格式
{
   ... 
   "data":{
     "title":"New Notification",
     "body":"Test 123"
   },
   ...
}

代替

{
   ...
   "notification":{
     "title":"New Notification",
     "body":"Test 123"
   },
   ...
} (if you use this way, phone will display DEFAULT Notification instead of YOUR CUSTOM NOTIFICATION when your app destroyed)