简体   繁体   English

如何在Django中创建推送通知?

[英]How to create push notifications in django?

I'd like to implement push notifications in android ? 我想在android中实现推送通知吗?

I prefer not to use an existing plugins 我不想使用现有的插件

would someone give me an example of code on how to send a message 有人会给我一个有关如何发送消息的代码示例

have been struggling since a week none of my tries have been successfull 一个星期以来一直在挣扎,我的尝试都没有成功

This is my last try: 这是我的最后尝试:

gcm.py gcm.py

    import requests
import json


def send_gcm_message(api_key, regs_id, data, collapse_key=None):
    """
    Send a GCM message for one or more devices, using json data
    api_key: The API_KEY from your console (https://code.google.com/apis/console, locate Key for Server Apps in
        Google Cloud Messaging for Android)
    regs_id: A list with the devices which will be receiving a message
    data: The dict data which will be send
    collapse_key: A string to group messages, look at the documentation about it:
        http://developer.android.com/google/gcm/gcm.html#request
    """
    values = {
        'registration_ids': regs_id,
        'collapse_key': collapse_key,
        'data': data
    }

    values = json.dumps(values)

    headers = {
        'UserAgent': "GCM-Server",
        'Content-Type': 'application/json',
        'Authorization': 'key=' + api_key,
    }

    response = requests.post(url="https://android.googleapis.com/gcm/send",
                             data=values,
                             headers=headers)
    return response.content

views.py views.py

import settings
from .gcm import send_gcm_message


@render_to("push/envoyer_message.html")
def send_message(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect('/client/login')
    else:
        if request.method == 'POST':
            form = MessageForm(request.POST)
            if form.is_valid():
                form.save()

                reg_id='APA91bFSIEHPOeT2I7ddCqnYtnJ-iAEQfCiR3HArUIz0t5lQUVfIOGhlEIUJCJLY7SWEdwiJCHedLNaSFi6oVqbnsci9-HmpBNiZAa86KD3349AIWMesweUqF2YbfpSBRot1tNLCITRFbYH9g5AO514s8Zzs4ABumA'
                msg='test'
                send = send_gcm_message(api_key = settings.GCM_APIKEY, regs_id=[reg_id], data={'msg': msg},collapse_key="message")

                messages.success(request, _(u'Formulaire envoyé avec succès.'))
                return HttpResponseRedirect(reverse("push-confirmation-envoi"))
        else:
            form = MessageForm()        
        return locals()

EDIT: 编辑:

This is the error I get : 这是我得到的错误:

 Field "data" must be a JSON array: test www

SOLVED 解决了

                regs_id = list()
                for device in devices_a :
                    regs_id.append(device.token_string)                    


                message = json.dumps(message)
                values = {
                    'registration_ids': regs_id,
                    'collapse_key': "message" ,
                    'data': {"message":str(msg.message)}
                }   

                headers = {
                    'UserAgent': "GCM-Server",
                    'Content-Type': 'application/json',
                    'Authorization': 'key=' + settings.GCM_APIKEY,
                }

                response = requests.post(url="https://android.googleapis.com/gcm/send",data=json.dumps(values), headers=headers)

                r = json.loads(response.content)
                msg.nbr_android_recieved = r["success"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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