Seite 1 von 1

PHP zu PB Code - firebase push notification

Verfasst: 12.07.2020 14:57
von String
Das PHP Skript funktioniert.
In PB scheint alles ok, außer ein Eintrag. Aber ich finde den Fehler nicht!
Bekomme von firebase immer die Meldung!
"registration_ids" field is Not a JSON Array"
(Der Fehler erscheint natürlich nur mit den richtigen Key, Token und Link)

Wäre super nett wenn mir da jemand helfen könnte! Danke!
Beide Codes lassen sich direkt ausführen mit "httpbin.org"
Den einzigen unterschied den ich feststellen konnte war der.

; PHP OK = "registration_ids": [ "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" ]
; PB ERROR = "registration_ids": "[\"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"]"

Aber wie behebe ich den ? Irgendwas gehe ich da immer falsch an.


PHP:

Code: Alles auswählen

<?php
	$key     = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
	$token   = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
	$path_to_firebase_cm = "https://httpbin.org/post";
    # $path_to_firebase_cm = 'https://fcm.googleapis.com/fcm/send';

	$token=array($token);
	$fields = array(
		'registration_ids' => $token,
		'priority' => 10,
		'notification' => array('title' => $type_of_notification, 'body' => $title ,'sound'=>'Default'),
	);
	
	$headers = array(
		'Authorization:key=' .$key ,
		'Content-Type:application/json'
	);  

	$ch = curl_init($path_to_firebase_cm); 
	curl_setopt($ch, CURLOPT_URL, $path_to_firebase_cm); 
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
	curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); 
	$result = curl_exec($ch);     
	curl_close($ch);
	print "<body text='blue'>";	
	$show = str_replace(",", ",<br>", $result);	
	print $show;
	print ("<br>"."<br>"."<br>");
	return $result;
?>
PB v5.72x86:

Code: Alles auswählen

InitNetwork()
#JSON_DATA = 1
#JSON_TOKEN = 2

key$     = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
token$   = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
path_to_firebase_cm$ = "https://httpbin.org/post";
; path_to_firebase_cm$ = "https://fcm.googleapis.com/fcm/send"

If CreateJSON(#JSON_DATA)
  Person = SetJSONObject(JSONValue(#JSON_DATA))  
  If CreateJSON(#JSON_TOKEN) 
    ArrayValue = SetJSONArray(JSONValue(#JSON_TOKEN))
    NumValue = AddJSONElement(ArrayValue) 
    SetJSONString(NumValue, token$)
    SetJSONString(AddJSONMember(Person, "registration_ids"), ComposeJSON(#JSON_TOKEN))   
    SetJSONInteger(AddJSONMember(Person, "priority" ), 10)  
    Values =  SetJSONObject(AddJSONMember(Person, "notification"))
    SetJSONString(AddJSONMember(Values, "body" ), "")
    SetJSONString(AddJSONMember(Values, "sound" ), "Default")
    SetJSONString(AddJSONMember(Values, "title" ), "")  
    NewMap Header$()
    Header$("ContentType") = "text/plain"
    Header$("Authorization") = "key=" + key$
    Header$("Content-Type") = "application/json"
    HttpRequest = HTTPRequest(#PB_HTTP_Post, path_to_firebase_cm$, ComposeJSON(#JSON_DATA), 0, Header$())
    If HttpRequest
      Debug HTTPInfo(HTTPRequest, #PB_HTTP_Response)
      FinishHTTP(HTTPRequest)
    EndIf       
  EndIf 
EndIf
End

Re: PHP zu PB Code - firebase push notification

Verfasst: 12.07.2020 18:00
von Andesdaf
Du erstellst eigene JSON-Daten für das Token und fügst dessen komplette String-Repräsentation dann
in übergeordnete JSON-Daten ein - logischerweise werden die Hochkommas dabei escapt, ansonsten
würde das Format nicht mehr passen. Wieso das Array nicht direkt in den Person-Daten einfügen?

Code: Alles auswählen

If CreateJSON(#JSON_DATA)
  Person = SetJSONObject(JSONValue(#JSON_DATA)) 
  ArrayValue = SetJSONArray(AddJSONMember(Person, "registration_ids"))
  NumValue = AddJSONElement(ArrayValue)
  SetJSONString(NumValue, token$)
  SetJSONInteger(AddJSONMember(Person, "priority" ), 10) 
  Values =  SetJSONObject(AddJSONMember(Person, "notification"))
  SetJSONString(AddJSONMember(Values, "body" ), "")
  SetJSONString(AddJSONMember(Values, "sound" ), "Default")
  SetJSONString(AddJSONMember(Values, "title" ), "")
  Debug ComposeJSON(#JSON_DATA)
EndIf

Re: PHP zu PB Code - firebase push notification

Verfasst: 12.07.2020 18:15
von String
Danke, Danke! Du hast mir echt weiter geholfen!
Das war mein erster JSON versuch mit PB :bounce: