在Firebase上为参数创建自定义数据以调用推送通知 [英] Make a custom data for parameter on firebase to call on push notification

查看:73
本文介绍了在Firebase上为参数创建自定义数据以调用推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从通过FCM onMessageReceived方法从RemoteMessage中获取价值解决我的问题,但无济于事

I researched from Get value from RemoteMessage from FCM onMessageReceived method for my problem but not helped

我想在Firebase中创建自定义数据,以便在我的推送通知中调用该数据.至少该数据可以调用onMessageReceived方法.

I want to create custom data in Firebase to call that data on my push notification. At least that data can call on onMessageReceived method.

我的网络服务

$msg = array
(
    'body'      => "$message",
    'title'     => "Approval System",
    'sound'     => 'default',
    'vibrate'   => 1,
    'largeIcon' => 'large_icon',
    'smallIcon' => 'small_icon'

);
$fields = array
(
    'registration_ids'  => $tokens,
    'questionTitle'     => "Test",
    'notification'      => $msg
    );


$headers = array(
    'Authorization:key = XXX',
    'Content-Type: application/json'
    );

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);  
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);           
if ($result === FALSE) {
    die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;

和我的onMessageReceived方法

and my onMessageReceived method

Bundle bundle = new Bundle();
bundle.putString("DATA",messageBody);
Intent intent = new Intent(FirebaseMessage.this, ApprovalSystemActivity.class);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT
);

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notif)
        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.notif))
        .setSound(defaultSoundUri)
        .setStyle(new NotificationCompat.BigTextStyle()
                .bigText(messageBody))
        .setAutoCancel(true)
        .setContentTitle("Approval System")
        .setContentText(messageBody)
        .setContentIntent(pendingIntent);

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

notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

我如何在$ fields数组上调用"questionTitle"到Java android?对不起我的问题,如果无法理解

how to i can call "questionTitle" on $fields array to java android ? sorry for my question if cannot understand

谢谢

推荐答案

您必须更改JSON结构.将"questionTitle"字段放在数据"对象中

You have to change your JSON structure. Put the "questionTitle" field inside a "data" object

{
  "registration_ids": [
    "dTlWTEzmJZk:APA91bF2COvY_WPcybE6okJv2a‌​9qUkEUieDOP6q_Q204q1‌​JMz-l2Un49M5cPg4WzE5‌​ODTFyU_82Sz5lvR2zGO6‌​Al_51lY1AyfEuw890jZ9‌​YZ5pWn8kUjClAEwNjxBZ‌​8eELQABRndhQom"
  ],
  ‌​"notification": {
    "bod‌​y": "xoa",
    "title": "Ap‌​proval System",
    "sound": "default",
    "vibrate": 1,
    "largeIcon": "large_ico‌​n",
    "smallIcon": "smal‌​l_icon"
  },
  "data": {
    "qu‌​estionTitle": "Test"
  }
}

要使用此JSON,请将您的php修改为:

To make this JSON modify your php to:

$msg = array
 (
    'body'      => "$message",
    'title'     => "Approval System",
    'sound'     => 'default',
    'vibrate'   => 1,
    'largeIcon' => 'large_icon',
    'smallIcon' => 'small_icon'

);

$dat = array
(
    'questionTitle'     => "Test"
);

$fields = array
(
    'registration_ids'  => $tokens,
    'notification'      => $msg,
    'data'       => $dat
);

然后在您的Service类别中通过

And then in your Service class get it by

String questionTitle = remoteMessage.getData().get("questionTitle");

这篇关于在Firebase上为参数创建自定义数据以调用推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆