我尝试使用以下代码将图片发送到我的安卓应用程序。
public class PushNotificationUtility {
private static String SERVER_KEY = "MYSERVERKEY";
public static void main(String[] args) throws Exception {
String title = "My First Notification";
String message = "Hello, I'm push notification";
sendPushNotification(title, message);
}
private static void sendPushNotification(String title, String message) throws Exception {
// Create connection to send FCM Message request.
URL url = new URL("https://fcm.googleapis.com/fcm/send");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", "key=" + SERVER_KEY);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestMethod("POST");
conn.setDoOutput(true);
JSONObject json = new JSONObject();
json.put("to","/topics/MYTopic");
JSONObject info = new JSONObject();
info.put("title", title); // Notification title
info.put("body", message); // Notification body
info.put("picture","http://36.media.tumblr.com/c066cc2238103856c9ac506faa6f3bc2/tumblr_nmstmqtuo81tssmyno1_1280.jpg");
info.put("summaryText","Summary");
json.put("notification", info);
// Send FCM message content.
OutputStream outputStream = conn.getOutputStream();
outputStream.write(json.toString().getBytes());
System.out.println(conn.getResponseCode());
System.out.println(conn.getResponseMessage());
}
}
我可以将通知发送到Android设备,但不能发送图片。我想将图片与消息一起发送。在客户端上我必须做什么才能显示图片吗?
如何解决此问题?
FCM 通知上没有picture
属性,您可以做的是使用 data
属性发送和对象要在应用中使用的重要数据 当用户收到推送时,使用属性picture
图片链接创建它:
let data = {
picture: 'http://36.media.tumblr.com/c066cc2238103856c9ac506faa6f3bc2/tumblr_nmstmqtuo81tssmyno1_1280.jpg'
};
info.put("data", data); // Notification data
在接收推送方法中,您可以抓取此属性并显示图片。
myPlugin.on('notification', notification => {
console.log(notification); // JUST TO SEE THE CALLBACK STRUCTURE
this.myImage = notification.data.picture;
});
希望这对:D有所帮助