简体   繁体   中英

Python GCM send push notification to iOS & Android topics with Django

I'm having some trouble sending push notifications to Android and iOS devices using Django + GCM.


Problem 1: Android receive push when in background but the method onMessageReceived is not called. So I can't handle the received data. iOS only receive push if the app is opened or minimized, if the app is closed, do not received pushs.

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

Django code:

@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)

Problem 2: The Android received pushs in background and the method onMessageReceived is called (Android OK). But the iOS app does not receive any notifications, opened, closed, minimized. Just wont work.

Django code:

@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) 

可能不是您要找的东西,而是将django-pushnotifications之类的库与github.com上提供的android Gcm示例一起使用对我来说很好。

Solution/Approach for #1, which I have used.

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)

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