如何将推送通知中的图片发送到离子2应用程序

时间:2017-10-13 12:29:17

标签: cordova push-notification ionic2 google-cloud-messaging cordova-plugins

我尝试使用以下代码将图片发送到我的Android应用程序。

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设备,但不能发送图片。我想发送图片和消息。在客户端有什么我需要做的才能显示图片吗?

如何解决此问题?

1 个答案:

答案 0 :(得分:0)

FCM notification上没有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