简体   繁体   中英

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.

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.

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'),
);

Use ApnsConfig to show alerts in iOS when the app is in background. This worked for me.

var pushMessage = new FcmMessage
        {
            Data = new Dictionary<string, string>
                    {
                        { Constants.PushMessages.MessageId, $"{message.MessageId}" },
                        { Constants.PushMessages.TopicId, $"{message.TopicId}" },
                        { Constants.PushMessages.Type, $"{message.Type}" },
                        { Constants.PushMessages.Title, message.Title },
                        { Constants.PushMessages.Content, message.Content },
                        { Constants.PushMessages.CreatedBy, message.CreatedBy },
                        { Constants.PushMessages.CreatedOn, $"{message.CreatedOn}" }
                    },
            Token = registrationToken,
            Apns = new ApnsConfig
            {
                Aps = new Aps
                {
                    Alert = new ApsAlert
                    {
                        Title = message.Title,
                        Body = message.Content
                    }
                }
            }
        };
        await FirebaseMessaging.DefaultInstance.SendAsync(pushMessage);

I'm having the same issue and have been struggling with it for a week. Has anyone found the solution to this?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM