0

I'd like to implement push notifications in 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

    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

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

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"]
Armance
  • 5,350
  • 14
  • 57
  • 80
  • Make some research on Google cloud Messaging – meda Nov 27 '13 at 17:27
  • It's not duplicate,my question is in django plus the question you referred to is in 2009 it's obsolete because now there is GCM !! – Armance Nov 27 '13 at 17:29
  • @meda I already did of course but in django i dont know how to implement it ,is there some functions i can use? – Armance Nov 27 '13 at 17:30
  • please check this https://github.com/bogdal/django-gcm – meda Nov 27 '13 at 17:31
  • thanks,I took the send message function and tried it inside my code but it didnt work (see my adited question) although I got a success responce (send) : `{"multicast_id":9194922683039263767,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1385573990077514%2cad0c7a38eb0007"}]}` – Armance Nov 27 '13 at 17:43
  • The question is closed, post a new question with this code, it will be a valid question – meda Nov 27 '13 at 18:29
  • @meda this is the new one : http://stackoverflow.com/questions/20262597/how-to-create-push-notifications-in-django – Armance Nov 28 '13 at 09:46
  • For sending Push via FCM use lightweight library github.com/karanatwal/FirePush – karanatwal.github.io Jun 10 '19 at 15:30

0 Answers0