Python gcm and Django : response is always empty - android

I am using python gcm for Django.response=json.dumps(gcm.json_request(registration_ids=reg_ids,data=data)).response is always NULL. But message is sent
Their documentation suggested:
response=json.dumps(gcm.json_request(registration_ids=reg_ids,data=data))
Well! Message is received to mobile successfully. But the problem is response value is always empty.
My code:
def sendNotification(request):
gcm = GCM('AIzXXXXXXXXXXXXXXXXXXX')
reg_ids=[]
devices=Devices.objects.all()
for device in devices:
reg_ids.append(device.gcm_reg_id)
data = {'sender': "someone",'message':"Cool!",}
response=json.dumps(gcm.json_request(registration_ids=reg_ids,data=data))
return HttpResponse(response)

Related

Smack Push Notification not working

I am using Smack library to get XMPP pubsub requests in my Android App.
I want to send alert from 1 phone which is received on another phone as push notification popup.
On debugging, control returns null due to this code in Smack 4.2.0 library's ChatManager.java class.
EntityJid userJID = from.asEntityJidIfPossible();
if (userJID == null) {
LOGGER.warning("Message from JID without localpart: '" + message.toXML() + "'");
return null;
}
My from isnt null,ofc... but, my asEntityjidIfPossibe is null.
My Iq is as follows:
<iq
to="pubsub.foo"
from="notifier#foo"
type="set"
id="1523272621323">
<pubsub xmlns="http://jabber.org/protocol/pubsub">
<publish xmlns="http://jabber.org/protocol/pubsub"
node="/home/foo/notifier/c7215880-a36e-4cb9-bdd3-441cf9c29c31/pqr">
<item id="item11523272621323">
<json xmlns="urn:xmpp:json:0">
{"id":"b7da305d-77c2-422a-9c95-7ec8a347192e",
"orgId":"c7215880-a36e-4cb9-bdd3-441cf9c29c31",
"user_id":"f0622ed6-860f-4f4e-9b11-c4ab6cb8cd66",
"username":"xyz",
"subject":"Message",
"body":"hello all",
"priority":"NORMAL",
"ack_required":false,
"users":["pqr"],
"replyTo":["/home/foo/notifier/c7215880-a36e-4cb9-bdd3-441cf9c29c31/xyz",
"/home/foo/notifier"]}
</json>
</item>
</publish>
</pubsub>
</iq>
All I want is to see if message sent is recieved or not? And how to get data to process it...Data is sent perfectly on the server from the sending phone.
On recieving device,
I get this message TWICE,
Message from JID without localpart: '<message to='xyz#foo/5301412210663914056363834' from='pubsub.foo'><event xmlns='http://jabber.org/protocol/pubsub#event'><items node='/home/foo/notifier/c7215880-a36e-4cb9-bdd3-441cf9c29c31/xyz'><retract id='item11523351450156'/></items></event></message>'

Node.js - null but authorised

I got this Unauthorized null message when I try trigger node script for my push notification.
I'm using this sample code for my push notification.
https://github.com/hollyschinsky/PushNotificationSample30/
Please refer this site for your reference.
http://devgirl.org/2013/07/17/tutorial-implement-push-notifications-in-your-phonegap-application/
I already check this solution but it still didn't work. Why?
node.js returns null push messages
After I insert the correct API key, we got authorised but null.
Actually "null" in the case means it is a success. The problem is when you run your application in local environment and the device is connected to wifi. There is a certain case that firewall block the traffic from the outgoing connection ports which are used by GCM (5228,5229,5230).
You can refer to the site as a reference
http://developer.android.com/google/gcm/http.html
Looks like you have not registered for an api key. This is from the url you posted.
var gcm = require('node-gcm');
var message = new gcm.Message();
//API Server Key
var sender = new gcm.Sender('AIzaSyCDx8v9R0fMsAsjoAffF-P3FCFWXlvwLhg');
var registrationIds = [];
// Value the payload data to send...
message.addData('message',"\u270C Peace, Love \u2764 and PhoneGap \u2706!");
message.addData('title','Push Notification Sample' );
message.addData('msgcnt','3'); // Shows up in the notification in the status bar
message.addData('soundname','beep.wav'); //Sound to play upon notification receipt - put in the www folder in app
//message.collapseKey = 'demo';
//message.delayWhileIdle = true; //Default is false
message.timeToLive = 3000;// Duration in seconds to hold in GCM and retry before timing out. Default 4 weeks (2,419,200 seconds) if not specified.
// At least one reg id required
registrationIds.push('APA91bwu-47V0L7xB55zoVd47zOJahUgBFFuxDiUBjLAUdpuWwEcLd3FvbcNTPKTSnDZwjN384qTyfWW2KAJJW7ArZ-QVPExnxWK91Pc-uTzFdFaJ3URK470WmTl5R1zL0Vloru1B-AfHO6QFFg47O4Cnv6yBOWEFcvZlHDBY8YaDc4UeKUe7ao');
/**
* Parameters: message-literal, registrationIds-array, No. of retries, callback-function
*/
sender.send(message, registrationIds, 4, function (result) {
console.log(result);
});

How to send a notification for a particular android device by GCM through Windows azure?

I had registered android device by GCM and registration id of device is registered with Windows azure which gives an another unique id for that device. Now my doubt is how to send notification to only one device by the registration id given by windows azure(not by GCM registration id)? I was referred the following link.http://msdn.microsoft.com/en-us/library/windowsazure/dn223273.aspx
URL specified as follows in that link. Here i know namespace,Notification hub and messages but where i have to give that unique registration id of device given by windows azure for sending notification to that device
https://{namespace}.servicebus.windows.net/{NotificationHub}/messages/?api-version=2013-08
I had used the following code in java
String strAzureURL = "https://namespaceName.servicebus.windows.net/hubName/testMessage";
String strAzureRegistrationId = "Tag1";
String strSharedAccessSignature = "MySharedAccessSignature";
String strSharedAccessName = "MySharedAccessName";
String strAuthorizationToken = URLDecoder.decode(strSharedAccessName+":"+strSharedAccessSignature,"UTF-8");
String strAuthWrapFormat = "WRAP access_token=\""+strAuthorizationToken+"\"";
String info = null;
HttpClient client = new HttpClient();
try {
GetMethod method = new GetMethod(strAzureURL);
method.setRequestHeader("Authorization", strAuthWrapFormat);
method.setRequestHeader("ServiceBusNotification-Tags", strAzureRegistrationId);
method.setRequestHeader("ServiceBusNotification-Format", "gcm");
method.setRequestHeader("Audience","https://namespaceName.servicebus.windows.net");
client.executeMethod(method);
info = method.getResponseBodyAsString();
System.out.println("Response "+info);
} catch (UnknownHostException e1) {
e1.printStackTrace();
throw e1;
}
Now i am receiving the following error.
Response <Error><Code>401</Code>
<Detail>MissingAudience: The provided token does not specify the 'Audience'..TrackingId:75c1d492-3ddf-4d88-84b7-1c2f07d9623b_G5,TimeStamp:1/8/2014 9:57:39 AM</Detail>
</Error>
Thanks for any help !
Take a look at this page: http://msdn.microsoft.com/en-us/library/dn282661.aspx. It includes all of the steps for registering your client application with GCM and then with Notification Hubs. From the client side, it registers with the Notification Hub and set's it's "MyTag" to to be the Tag. What you'll want to do is register with a tag set to the registration ID. So from the client side:
hub.register(registrationId, registrationId, <Optional Other Tags>);
Then from wherever you're triggering the push notification, you would pass in the registration ID you want to deliver to as the ServiceBusNotification-Tags (as seen in the REST API link you provided). That will then cause the Notification Hub to deliver a message to only devices that have registered with the tag.

On rapns, how can I know the token for unregistered GCM devices so I can remove them from my database?

I am using rapns to provide GCM and APNS support. For APNS, I know what unregistered device I must delete via on.apns_feedback (rapns.rb):
on.apns_feedback do |feedback|
device = AppleDevice.find_by_token(feedback.device_token)
device.destroy if device
end
but for GCM, I can't find a way to know what device is unregistered so I can delete it from my database.
I tried with the reflection API, but I'm not getting on.notification_failed and on.error called whenever a Rapns::DeliveryError exception is raised and those methods doesn't seem to give me a way to know the unregistered tokens.
I tried catching the Rapns::DeliveryError, but it doesn't seem to work.
messenger = PushMessenger::Gcm.new
GoogleDevice.find_in_batches :batch_size => 1000 do |devices|
tokens = devices.map(&:token)
begin
messenger.deliver(app, tokens, payload, nil, true)
rescue Rapns::DeliveryError => error
GoogleDevice.destroy_all # Just to see it works
end
end
PushMessenger:
module PushMessenger
class Gcm
def deliver(app, tokens, payload, collapse_key=nil, delay_while_idle=nil, expiry=1.day.to_i)
tokens = *tokens
n = Rapns::Gcm::Notification.new
n.app = app
n.registration_ids = tokens
n.collapse_key = collapse_key
n.delay_while_idle = delay_while_idle
n.expiry = expiry
n.data = payload
n.save!
end
end
end
How can I know the token for these unregistered devices so I can remove them from my database?
I'm using rapns to do pretty much the same, so here my two cents:
First, for Android devices you don't need the device_token to deactivate/remove the device (On GCM device_token == registration_id). You can get that registration_id from the on.notification_failed callback with the Reflection API.
Last, which version of rapns are you using? Right now the last version (3.4.1) has a bug with on.notification_failed, but version 3.3.2 works just fine and you'll be able to do something like:
on.notification_failed do |notification|
device = Device.find_by_token(notification.registration_ids.first)
device.destroy if device
end
Hope that helps.

Post push notification to C2DM (android) using Django

I found how to send push notification to Android device using Django here (here is the code).
So adopted that and my code looks like this:
def sendAndroidPushNotification(registration_id, collapse_key, a, b) :
try:
auth = getNewAndroidAuthorizationToken() # this works I'm fetching new token so this is up to date (its length is 267 characters)
push_request = urllib2.Request("https://android.apis.google.com/c2dm/send")
data = urllib.urlencode({'data.testA' : a,
'data.testB' : b,
'collapse_key' : collapse_key,
'registration_id': registration_id
})
push_request.add_data( data )
push_request.add_header('Authorization', 'GoogleLogin auth=' + auth)
push_request.add_header('Content-Type', 'application/x-www-form-urlencoded')
push_request.add_header('Content-Length', len(data))
urllib2.build_opener().open(push_request)
except urllib2.HTTPError as e:
print 'got exception during push notification'
print 'Reason: "{0}" code: {1}'.format(e.reason, e.code)
pass
this give me error: "Reason: "Unauthorized" code: 401" (at some point it was 403). Version which uses httplib.HTTPSConnection instead of urllib2.Request has same problem.
It looks almost the same as code shown here so I'm totally confused. What I'm doing wrong?
Edit:
Just in case, here is how I fetch authorization token (it looks like that it works fine), maybe my parsing is wrong:
def getNewAndroidAuthorizationToken() :
request = urllib2.Request("https://www.google.com/accounts/ClientLogin")
data = urllib.urlencode({'accountType' : 'HOSTED_OR_GOOGLE',
'Email' : 'someaccount#gmail.com',
'Passwd' : 'asdjsdfa',
'service' : 'ac2dm',
'source' : 'com.mycompany.mypackage',})
request.add_data(data)
content = urllib2.build_opener().open(request)
lines = content.readlines()
for line in lines :
if line.find("Auth=")==0 :
return line[5:]
return
C2DM is deprecated. Developers are encouraged to switch to GCM, C2DM will be supported for a short time. Simple API instead of ClientLogin and oAuth2 which are not supported.
http://developer.android.com/guide/google/gcm/index.html

Categories

Resources