Last active
August 29, 2015 14:06
-
-
Save ChrisRisner/0bb1f83917cdf4c35dea to your computer and use it in GitHub Desktop.
Android Push Notifications with Notification Hubs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.get = function(request, response) { | |
var azure = require('azure'); | |
var notificationHubService = azure.createNotificationHubService('NotificationHubName', | |
'NotificationHubFullSharedAccessSignature'); | |
notificationHubService.gcm.send(null, | |
'{"data":{"msg" : "Hello from Mobile Services!"}}' | |
, | |
function (error) | |
{ | |
if (!error) { | |
console.warn("Notification successful"); | |
} | |
} | |
); | |
response.send(statusCodes.OK, { message : 'Notification Sent' }); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<uses-sdk | |
android:minSdkVersion="16" | |
android:targetSdkVersion="19" /> | |
<uses-permission android:name="android.permission.INTERNET"/> | |
<uses-permission android:name="android.permission.GET_ACCOUNTS"/> | |
<uses-permission android:name="android.permission.WAKE_LOCK"/> | |
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> | |
<permission android:name="PACAKGE.permission.C2D_MESSAGE" android:protectionLevel="signature" /> | |
<uses-permission android:name="PACKAGE.permission.C2D_MESSAGE"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<receiver android:name="com.microsoft.windowsazure.notifications.NotificationsBroadcastReceiver" | |
android:permission="com.google.android.c2dm.permission.SEND"> | |
<intent-filter> | |
<action android:name="com.google.android.c2dm.intent.RECEIVE" /> | |
<category android:name="PACKAGE" /> | |
</intent-filter> | |
</receiver> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyHandler extends NotificationsHandler { | |
public static final int NOTIFICATION_ID = 1; | |
private NotificationManager mNotificationManager; | |
Context ctx; | |
@Override | |
public void onReceive(Context context, Bundle bundle) { | |
ctx = context; | |
String nhMessage = bundle.getString("msg"); | |
sendNotification(nhMessage); | |
} | |
private void sendNotification(String msg) { | |
mNotificationManager = (NotificationManager) | |
ctx.getSystemService(Context.NOTIFICATION_SERVICE); | |
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, | |
new Intent(ctx, MainActivity.class), 0); | |
NotificationCompat.Builder mBuilder = | |
new NotificationCompat.Builder(ctx) | |
.setSmallIcon(R.drawable.ic_launcher) | |
.setContentTitle("Notification Hub Demo") | |
.setStyle(new NotificationCompat.BigTextStyle() | |
.bigText(msg)) | |
.setContentText(msg); | |
mBuilder.setContentIntent(contentIntent); | |
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); | |
int duration = Toast.LENGTH_SHORT; | |
Toast toast = Toast.makeText(ctx, msg, duration); | |
toast.show(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mGcm = GoogleCloudMessaging.getInstance(getActivity()); | |
String connectionString = "NotificationHubListenSharedAccessSignature"; | |
mHub = new NotificationHub("NotificationHubName", connectionString, getActivity()); | |
NotificationsManager.handleNotifications(getActivity(), SENDER_ID, MyHandler.class); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SuppressWarnings("unchecked") | |
private void registerWithGcm() { | |
new AsyncTask() { | |
@Override | |
protected Object doInBackground(Object... params) { | |
try { | |
mRegistrationId = mGcm.register(SENDER_ID); | |
Log.i(TAG, "Registered with id: " + mRegistrationId); | |
} catch (Exception e) { | |
return e; | |
} | |
return null; | |
} | |
protected void onPostExecute(Object result) { | |
lblRegistration.setText(mRegistrationId); | |
lblStatus.setText(getResources().getString(R.string.status_registered)); | |
}; | |
}.execute(null, null, null); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private OnClickListener registerWithTemplates = new OnClickListener() { | |
@SuppressWarnings("unchecked") | |
@Override | |
public void onClick(View v) { | |
Log.i(TAG, "Tapped register with templates"); | |
new AsyncTask() { | |
@Override | |
protected Object doInBackground(Object... params) { | |
try { | |
mHub.registerTemplate(mRegistrationId, "messageTemplate", | |
"{\"data\":{\"msg\":\"$(message)\"}, \"collapse_key\":\"$(collapse_key)\"}", | |
"MyTag", "AllUsers", "AndroidUser"); | |
} catch (Exception e) { | |
Log.e(TAG, "Issue registering with hub with template: " + e.getMessage()); | |
return e; | |
} | |
return null; | |
} | |
protected void onPostExecute(Object result) { | |
lblStatus.setText(getResources().getString(R.string.status_registered_with_templates)); | |
}; | |
}.execute(null, null, null); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.get = function(request, response) { | |
var azure = require('azure'); | |
var notificationHubService = azure.createNotificationHubService('NotificationHubName', | |
'NotificationHubFullSharedAccessSignature'); | |
notificationHubService.gcm.send('MyTag', | |
'{"data":{"msg" : "Hello MyTag!"}}' | |
, | |
function (error) | |
{ | |
if (!error) { | |
console.warn("Notification successful"); | |
} | |
} | |
); | |
response.send(statusCodes.OK, { message : 'Notification Sent' }); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.get = function(request, response) { | |
var azure = require('azure'); | |
var notificationHubService = azure.createNotificationHubService('NotificationHubName', | |
'NotificationHubFullSharedAccessSignature'); | |
var payload = '{ "message" : "Template push to everyone!", "collapse_key" : "Message" }'; | |
notificationHubService.send(null, payload, | |
function(error, outcome) { | |
console.log('issue sending push'); | |
console.log('error: ', error); | |
console.log('outcome: ',outcome); | |
}); | |
response.send(statusCodes.OK, { message : 'Notification Sent' }); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment