我正在编写一个从Firebase接收云消息并拨打指定电话号码或显示指定通知的应用程序。但是,当应用程序打开时,通知不会显示,而当应用程序关闭时,通知始终会发送通知(即使我发送电话指令时也是如此)。
我的服务:
公共类MyFireBaseMessagingService扩展了FirebaseMessagingService {
public static final String CHANNEL_ID = "channel01";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String message = remoteMessage.getNotification().getTitle();
if (message.equals("call"))
call(remoteMessage.getNotification().getBody());
if (message.equals("notify"))
notification("Hi!", remoteMessage.getNotification().getBody());
}
public void call(String phoneNumber)
{
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED)
{
Uri call = Uri.parse("tel:" + phoneNumber);
Intent surf = new Intent(Intent.ACTION_CALL, call);
startActivity(surf);
}
}
public void notification(String title, String body)
{
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle(title)
.setContentText(body);
Notification notification = builder.build();
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
}
}
打开应用程序后,它可以拨号,但无法显示通知。 让我感到困惑的是,即使当remoteMessage的标题是“ call”而不是“ notify”时,它也会在应用程序关闭时显示通知。