davibennun / laravel-push-notification

Laravel package to enable sending push notifications to devices
1.23k stars 259 forks source link

Sending the same JSON object to APNS/GCM #66

Open dror3go opened 9 years ago

dror3go commented 9 years ago

From my understanding, iOS (at least in some versions) takes care of the push notification in cases when the app is not open, and it requires a specific JSON format:

{
    "aps": {
        "alert": "Some text goes here",
        "sound": "default"
    }
}

If it gets this kind of object, iOS will display nicely the notification, with the sound and the defined text.

I want my code to be indifferent of the device (APNS/GCM) it sends to, so I prefer to send the same object to all. How can I send it as an object not decoded? When I tried it - Android devices didn't get any push notification.

<?php
$device = "Android"; // Android or iOS, dynamically.
$data = json_encode([
    'aps' => [
        'alert' => 'Text to see in the notification',
        'sound' => 'default'
    ]
]);
PushNotification::app('MyApp' . $device)
    ->to($to)
    ->send($data);

Am I missing something?

LiangJianle commented 9 years ago

your json is error; miss ']' it should be like this

$data = json_encode([ 'aps' => [ 'alert' => 'Text to see in the notification', 'sound' => 'default' ]]);

dror3go commented 9 years ago

That was just a typo in my example, @LiangJianle.

luisalvarez commented 8 years ago

the problem is that ZendService\Google\Gcm\Message use a toJson method to build the json for you and put everything that yoo send inside data key.

for use both we need to put in this way notification :{ body:'text', title:'text' } to see on ios but until now it is impossible, i looking to see i way to do that

ivankurn commented 7 years ago

Hello,

How to set 'aps' parameter inside the PushNotification::Message ?