0

Ich versuchte mit dem folgenden Code, um ein Bild an meine Android App zu senden.So senden Sie ein Bild in Push-Benachrichtigungen an ionic 2 App

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()); 
    } 
} 

Ich bin in der Lage, die Benachrichtigung an das Android-Gerät, aber nicht das Bild zu senden. Ich möchte das Bild zusammen mit der Nachricht senden. Gibt es irgendetwas, was ich auf der Client-Seite tun muss, um ein Bild anzuzeigen?

Wie kann ich dieses Problem beheben?

Antwort

0

Es gibt keine picture Eigenschaft auf FCM notification, was Sie können die data Eigenschaft zu tun ist, zum Senden und Objekt wichtiger Daten möchten Sie in Ihrer Anwendung verwenden, wenn der Benutzer einen Push empfängt, erstellen Sie es mit einer Eigenschaft picture mit Ihrem Bild link:

let data = { 
    picture: 'http://36.media.tumblr.com/c066cc2238103856c9ac506faa6f3bc2/tumblr_nmstmqtuo81tssmyno1_1280.jpg' 
}; 
info.put("data", data); // Notification data 

Und in Ihrer empfangenden Push-Methode können Sie diese Eigenschaft greifen und das Bild zeigen.

myPlugin.on('notification', notification => { 
    console.log(notification); // JUST TO SEE THE CALLBACK STRUCTURE 
    this.myImage = notification.data.picture; 
}); 

hoffe, das hilft: D

+0

Ich möchte Bild als Teil der Anmeldung wie eine Anzeige einiger Angebote nur angezeigt werden. Nicht wie einige Daten. Normalerweise sehen wir diese Art von Benachrichtigungen in Shopping-Apps. – user1188867

+0

Etwas wie dieses https://i.stack.imgur.com/5p4pa.png – user1188867

+0

@ user1188867 oh, hab es. Soweit ich weiß, unterstützt FCM cordova plugins diese Art der Benachrichtigung nicht. Ionic.io Benachrichtigungen unterstützt dies auch nicht. Ich denke, das ist mehr nativ oder hat noch niemand mit Plugins geschafft. Aber hier ist ein Android-Tutorial, wie Sie dies erreichen http://androidbash.com/firebase-push-notification-android/ –

Verwandte Themen