This is a migrated thread and some comments may be shown as answers.

Not receiving push notifications - nativescript

1 Answer 204 Views
Push Notifications
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
madson
Top achievements
Rank 1
madson asked on 04 Jan 2018, 06:48 PM

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";
    }
 
}

 

 

1 Answer, 1 is accepted

Sort by
0
Anton Dobrev
Telerik team
answered on 08 Jan 2018, 11:23 AM
Hi,

Thanks for posting to the Telerik Platform forums. 

My suggestion would be that you compare the payload sent from the Firebase console with this you are sending with your PHP script. After looking at the PHP code, you only include the data key in the payload which can then only be handled internally by the app via the plugin (it is not visualized automatically). My assumption is that you may need to include the notification key in the payload too. 

My advice would be to also consult the documentation of the plugin here - https://github.com/NativeScript/push-plugin/blob/master/README.md#receive-and-handle-messages-from-fcm-on-android and the Firebase documentation for the payload here - https://firebase.google.com/docs/cloud-messaging/concept-options

In addition, given that this forum is dedicated to Telerik Platform Backend Services questions, it would be more beneficial to post questions about NativeScript and third-party services (different from Telerik Platform Backend Services) in the respective NativeScript forums, plugin repositories or community forums. 

I hope that I was able to help. 

Regards,
Anton Dobrev
Progress Telerik
 
Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
 
Tags
Push Notifications
Asked by
madson
Top achievements
Rank 1
Answers by
Anton Dobrev
Telerik team
Share this question
or