Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background push notifications in Xamarin

Please consider the following issue:

I am using the following plugin for Xamarin to perform push notifications, I am attempting to get this running on both iOS and Androids.

https://github.com/CrossGeeks/FirebasePushNotificationPlugin

I am also using the following PHP code to send push notifications to the device from a website.

$fields = array
(
    'to' => $ID->notification_token,
    "content_available" => true,
    'priority'=>10,
    'notification' => array('title' => 'You have a new message', 'body' => 'Hooray', 'sound'=>'default', 'vibration'=>'default'),
);

$headers = array
(
    'Authorization: key=' . PUSH_NOTIFICATION_API_ACCESS_KEY,
    'Content-Type: application/json',
);

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/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;

Now the issue is on iOS I find that the push notification is reliable and runs perfectly when the app is in the foreground, but as soon as I background it (or lock the screen), they stop coming through until I foreground it again, then all of the notifications sent while it was in the background all come through at once.

I have enabled background processes in my info.plist as according to the plugins "Getting Started" guide.

I have tried changing content_availiable to content-availiable, also moving it into the notification array, also setting the value of it to true, 'true', 1 and '1'

I have also tried adding "alert" => "" to both the notification and the overall payload, however I still can't get it to work, outside of the foreground unlocked state.

I also notice that nothing appears to be triggering in the actual notification list (Pulled down from top of screen), the only way I know that the notifications are being received is from playing a certain sound file as it comes through and writing in the debug log.

Any assistance will be appreciated, thanks for your time.

like image 552
Aphire Avatar asked Jan 09 '18 11:01

Aphire


2 Answers

Make sure your message Json has the {"content-available" : "1", "alert" : ""} attributes if you want it to show up on iOS when not running in background.

As for Android, push notifications really are not 100% realiable, there are device specific impediments that may block your notification from showing up.

There might be a "test send" feature on Firebase (I've only used Azure Push Hub so far) so you can try and see if your message Json is configured correctly.

like image 187
Lucas Moura Veloso Avatar answered Oct 30 '22 19:10

Lucas Moura Veloso


I would try sending a Data Message instead

$fields = array
(
    'to' => $ID->notification_token,
    "content_available" => true,
    'priority'=>10,
    'data' => array('title' => 'You have a new message', 'body' => 'Hooray', 'sound'=>'default', 'vibration'=>'default'),
);
like image 23
Hichame Yessou Avatar answered Oct 30 '22 20:10

Hichame Yessou