This question is locked. New answers and comments are not allowed.
I'm sending push notifications with php script but I cannot receive them. I can register and get my token from fcm with success and
when I execute php script it returns success: 1 what means it's working but I don't receive any notifications in my real device.
If I send push from FCM console it works. The notifications are received but no sound can be heard.
payload:
<?php
include_once
(
"conn.php"
);
$sql
= mysqli_query(
$mysqli
,
"SELECT token FROM tokens"
);
$registration_IDs
=
array
();
while
(
$row
= mysqli_fetch_array(
$sql
)){
$registration_IDs
[] =
$row
[
'token'
];
}
$to
=
$registration_IDs
;
var_dump(
$to
);
echo
'<br><br>'
;
// $title = "$get_title";
// $message = "$get_message";
$title
=
"Title"
;
$message
=
"Push notifications message"
;
sendPush(
$to
,
$title
,
$message
);
function
sendPush(
$to
,
$title
,
$message
) {
// API access key from Google API's Console
// replace API
define(
'API_ACCESS_KEY'
,
'*****************************************'
);
$registrationIds
=
$to
;
$msg
=
array
(
'message'
=>
$message
,
'title'
=>
$title
// you can also add images, additionalData
);
$fields
=
array
(
'registration_ids'
=>
$registrationIds
,
'data'
=>
$msg
);
$headers
=
array
(
'Authorization: key='
. API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch
= curl_init();
curl_setopt(
$ch
,CURLOPT_URL,
'https://fcm.googleapis.com/fcm/send'
);
curl_setopt(
$ch
,CURLOPT_POST, true );
curl_setopt(
$ch
,CURLOPT_HTTPHEADER,
$headers
);
curl_setopt(
$ch
,CURLOPT_RETURNTRANSFER, true );
curl_setopt(
$ch
,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt(
$ch
,CURLOPT_POSTFIELDS, json_encode(
$fields
) );
$result
= curl_exec(
$ch
);
curl_close(
$ch
);
echo
"Result: "
.
$result
.
"<br><br>"
;
}
?>
Result: {"multicast_id":5775932825803865467,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1514317072210466%e17bdb85f9fd7ecd"}]}
home-view-model.ts:
import { Observable } from
"data/observable"
;
import * as pushPlugin from
"nativescript-push-notifications"
;
export class HomeViewModel extends Observable {
private pushSettings = {
// Android settings
senderID:
"*****************"
,
// Android: Required setting with the sender/project number
notificationCallbackAndroid: (stringifiedData: String, fcmNotification: any) => {
const notificationBody = fcmNotification && fcmNotification.getBody();
this
.updateMessage(
"Message received!\n"
+ notificationBody +
"\n"
+ stringifiedData);
},
// iOS settings
badge:
true
,
// Enable setting badge through Push Notification
sound:
true
,
// Enable playing a sound
alert:
true
,
// Enable creating a alert
notificationCallbackIOS: (message: any) => {
this
.updateMessage(
"Message received!\n"
+ JSON.stringify(message));
}
};
private _counter: number;
private _message: string;
constructor() {
super
();
this
.message =
""
;
this
.updateMessage(
"App started."
);
let self =
this
;
this
.onRegisterButtonTap();
}
get message(): string {
return
this
._message;
}
set message(value: string) {
if
(
this
._message !== value) {
this
._message = value;
this
.notifyPropertyChange(
"message"
, value);
}
}
onCheckButtonTap() {
let self =
this
;
pushPlugin.areNotificationsEnabled((areEnabled: Boolean) => {
self.updateMessage(
"Are Notifications enabled: "
+ !!areEnabled);
});
}
onRegisterButtonTap() {
let self =
this
;
pushPlugin.register(
this
.pushSettings, (token: String) => {
self.updateMessage(
"Device registered. Access token: "
+ token);
// token displayed in console for easier copying and debugging durng development
console.log(
"Device registered. Access token: "
+ token);
if
(pushPlugin.onMessageReceived) {
pushPlugin.onMessageReceived(
this
.pushSettings.notificationCallbackAndroid);
}
if
(pushPlugin.registerUserNotificationSettings) {
pushPlugin.registerUserNotificationSettings(() => {
self.updateMessage(
"Successfully registered for interactive push."
);
}, (err) => {
self.updateMessage(
"Error registering for interactive push: "
+ JSON.stringify(err));
});
}
}, (errorMessage: String) => {
self.updateMessage(JSON.stringify(errorMessage));
});
}
onUnregisterButtonTap() {
let self =
this
;
pushPlugin.unregister(
(successMessage: String) => {
self.updateMessage(successMessage);
},
(errorMessage: String) => {
self.updateMessage(JSON.stringify(errorMessage));
},
this
.pushSettings
);
}
private updateMessage(text: String) {
this
.message += text +
"\n"
;
}
}