PHP zu PB Code - firebase push notification

Für allgemeine Fragen zur Programmierung mit PureBasic.
Benutzeravatar
String
Beiträge: 69
Registriert: 17.05.2007 16:22

PHP zu PB Code - firebase push notification

Beitrag 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
PB v4.**
Andesdaf
Moderator
Beiträge: 2660
Registriert: 15.06.2008 18:22
Wohnort: Dresden

Re: PHP zu PB Code - firebase push notification

Beitrag 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
Win11 x64 | PB 6.00 (x64)
Benutzeravatar
String
Beiträge: 69
Registriert: 17.05.2007 16:22

Re: PHP zu PB Code - firebase push notification

Beitrag von String »

Danke, Danke! Du hast mir echt weiter geholfen!
Das war mein erster JSON versuch mit PB :bounce:
PB v4.**
Antworten