У меня есть код ниже в подпрограммах firebase. Когда получено уведомление, как мне отображать уведомление или данные в качестве оповещения? Мой php-код на моем сервере успешно отправляет сообщения на устройство Android по идентификатору, и звук появляется, когда сообщение получено, но я хотел бы отобразить предупреждение или передать фрагмент (я предпочитаю предупреждение).Как отобразить предупреждение из push-уведомления fcm
public class MyFirebaseMessagingService extends FirebaseMessagingService
{
private static final String TAG = "MyFirebaseMessService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
commonfunc.myprint("#####_____MyFirebaseMessService_from 1 : " + remoteMessage.getFrom());
if(remoteMessage.getData().size() > 0)
{
commonfunc.myprint("MyFirebaseMessService_getdata 2 : " + remoteMessage.getData());
Map<String, String> data = remoteMessage.getData();
String value1 = data.get("dtitle");
String value2 = data.get("dbody");
commonfunc.myprint("MyFirebaseMessService_getdata 2 : " + value1 + " " + value2);
sendNotification(remoteMessage.getData().toString());
}
if (remoteMessage.getNotification() != null)
{
commonfunc.myprint("MyFirebaseMessService_getNot 3 body: " + remoteMessage.getNotification().getBody());
commonfunc.myprint("MyFirebaseMessService_getNot 3 title: " + remoteMessage.getNotification().getTitle());
sendNotification(remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getTitle());
/*
final String mMessage = remoteMessage.getNotification().getBody();
Handler h = new Handler(Looper.getMainLooper());
h.post(new Runnable() {
public void run()
{
AlertDialog alertDialog = new AlertDialog.Builder(MyFirebaseMessagingService.this).create();
alertDialog.setTitle("Message");
alertDialog.setMessage("Message follows: " + mMessage);
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
return ;
}
});
*/
}
}
private void sendNotification(String body)
{
commonfunc.myprint("MyFirebaseMessService_sendNotification 4a sound ");
Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notifiBuilder = new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentTitle("Firebase Cloud Messaging")
.setContentText(body)
.setSound(notificationSound)
.setSmallIcon(life.poa.webcastman.poa1.R.drawable.common_google_signin_btn_icon_dark)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notifiBuilder.build());
commonfunc.myprint("MyFirebaseMessService_sendNotification 4b sound");
//Toast.makeText(getApplicationContext(), "____MyFirebaseMessagingService " + body, Toast.LENGTH_SHORT).show();
}
}
Вы получаете данные внутри onMessageReceived? –
Да, ниже мой php -and- я получаю как уведомление, так и данные в подпрограмме firebase и записываю их в журнал $ json_array = [ «уведомление» => [ «title» => «nTEST», «звук "=> "по умолчанию", "тело"=> "nollardata" ], "данные"=> [ "dtitle"=> "dTEST", "звук"=> "значение по умолчанию", "dbody" => "dollardata" ], "to" => $ target, "priority" => "high" ]; $ body = json_encode ($ json_array); –
Означает ли ваше уведомление данные? –