Reputation: 243
I'm trying to send push notifications to my iOS app with GCM. I tried to follow the (Google GCM) and (GCMServerDemo) for my app but I only can receive the notification printed on the xcode output: [body: hello, it's me, sound: default, collapse_key: do_not_collapse, badge: 2, from: 629354528047]
but nothing pop out on my testing phone. My server running on python and I have it implemented as:
from gcm import *
gcm= GCM("123...")
DEV_TOKEN = "l0NOTncXJXQ:APA91bGPVHxvF-PCL-PPNic6zhfnpv0aAe5KhvoYOOF_HfLZlCAquMGQb196J5_4zUEzWEirSOWP86d-n4-DJws4nPs5ZXR1c9UOQOPPuuCAjXFz2VIZ-5_SRz8G6D_MzKHv1W7yRrmZ"
reg_ids = [DEV_TOKEN]
notification = {"body": "hello, it's me", "sound": "default", "badge": 2}
response=gcm.json_request(registration_ids=reg_ids, data=notification)
print(response)
My AppDelegate on my app client to detect notification: http://swiftstub.com/621889661/
My ViewController: http://swiftstub.com/89730359
I want my client app receive the notification both when the app is active and when the screen is off (but right now I only can receive the message sent on my server in the xcode output screen, as [body: hello, it's me, sound: default, collapse_key: do_not_collapse, badge: 2, from: 629354528047]
)
I guess there is a problem when I parse the received message. Can anyone help me to fix it? Thank you.
Upvotes: 0
Views: 1325
Reputation: 2381
This is what the gcm payload should look like if all you want to do is display a message to the user:
{
"to":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"body":"This text message will be seen by the user",
"badge": 2,
"sound": "default"
}
}
It will only be visible by default when the app is not in the foreground. When it is, you need to read the message in your AppDelegate
's didReceiveRemoteNotification
method, and show it to the user yourself.
Edit:
Instead of using gcm.plaintext_request(registration_id=reg_id,data = data)
you might want to try this:
notification = {'body': "hello, it's me", "sound": "default", "badge": 2}
DEV_TOKEN = '**********************************'
reg_ids = [DEV_TOKEN]
response = gcm.json_request(registration_ids=reg_ids, notification=notification)
notification
should be outside the data key
Upvotes: 1