Python GCM向iOS&发送推送通知Django的Android主题

时间:2016-02-19 14:54:40

标签: android ios django google-cloud-messaging push

我在使用Django + GCM向Android和iOS设备发送推送通知时遇到了一些问题。

问题1:Android在后台接收推送,但未调用方法onMessageReceived。所以我无法处理收到的数据。 iOS仅在应用程序打开或最小化时才会收到推送,如果应用程序已关闭,则不会收到推送。

@Override
public void onMessageReceived(String from, Bundle data) {
   ....
}

Django代码:

@receiver(post_save, sender=Notification)
def send_push(sender,**kwargs):
n = kwargs.get('instance')
gcm = GCM("API_KEY")
data = {'messageContent': n.message, 'content_available':'true'}
notification = {'body': n.message, 'title': n.title,'sound':''}
topic = n.topic
gcm.send_topic_message(topic=topic, data=data,notification=notification)

问题2:Android在后台接收推送,调用方法onMessageReceived(Android OK)。但iOS应用程序不会收到任何通知,打开,关闭,最小化。不会工作。

Django代码:

@receiver(post_save, sender=Notification)
def send_push(sender,**kwargs):
n = kwargs.get('instance')
gcm = GCM("API_KEY")
data = {'messageContent': n.message, 'content_available':'true',
        'title': n.title, 'sound':'' }
topic = n.topic
gcm.send_topic_message(topic=topic, data=data) 

2 个答案:

答案 0 :(得分:0)

可能不是你想要的但是使用像gjubub.com上提供的android Gcm示例的django-pushnotifications这样的库对我很有帮助。

答案 1 :(得分:0)

解决方法/方法#1,我已经使用过。

from gcm import *

gcm = GCM("MY_GCM_KEY")
data = {'the_message': 'You have x new friends', 'param2': 'value2'}

reg_id = 'REG IDS'

gcm.plaintext_request(registration_id=reg_id, data=data)