Skip to content

Instantly share code, notes, and snippets.

@ChrisRisner
Last active December 21, 2015 14:48
Show Gist options
  • Save ChrisRisner/6321731 to your computer and use it in GitHub Desktop.
Save ChrisRisner/6321731 to your computer and use it in GitHub Desktop.
NotificationHubs
gcm = GoogleCloudMessaging.getInstance(this);
String connectionString = "<Notification Hub Listen Access Connection String>";
hub = new NotificationHub("<Notification Hub Name>", connectionString, this);
registerWithNotificationHubs();
<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="<your package>.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="<your package>.permission.C2D_MESSAGE"/>
<receiver
android:name=".MyBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="<Package Name>" />
</intent-filter>
</receiver>
private String SENDER_ID = "<Project ID from Google API Console>";
private GoogleCloudMessaging gcm;
private NotificationHub hub;
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
Context ctx;
@Override
public void onReceive(Context context, Intent intent) {
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
ctx = context;
String messageType = gcm.getMessageType(intent);
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification("Send error: " + intent.getExtras().toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification("Deleted messages on server: " +
intent.getExtras().toString());
} else {
sendNotification("Received: " + intent.getExtras().toString());
}
setResultCode(Activity.RESULT_OK);
}
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());
}
@SuppressWarnings("unchecked")
private void registerWithNotificationHubs() {
new AsyncTask() {
@Override
protected Object doInBackground(Object... params) {
try {
String regid = gcm.register(SENDER_ID);
hub.register(regid);
} catch (Exception e) {
return e;
}
return null;
}
}.execute(null, null, null);
}
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound];
- (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo {
NSLog(@"%@", userInfo);
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Notification" message:
[[userInfo objectForKey:@"aps"] objectForKey:@”alert”]
delegate:nil cancelButtonTitle:
@"OK" otherButtonTitles:nil, nil];
[alert show];
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *) deviceToken {
SBNotificationHub* hub = [[SBNotificationHub alloc] initWithConnectionString:
@"<Connection String>" notificationHubPath:@"<Notification Hub Name>"];
[hub registerNativeWithDeviceToken:deviceToken tags:nil completion:^(NSError* error) {
if (error != nil) {
NSLog(@"Error registering for notifications: %@", error);
}
}];
}
function testNotificationHub() {
var azure = require('azure');
var notificationHubService = azure.createNotificationHubService('<Notification Hub Name>',
'<Full Signature Connection String>');
notificationHubService.gcm.send(
null, '{"data":{"msg" : "Hello from Mobile Services!"}}',
function (error) {
if (!error) {
console.log("Notification successful");
}
}
);
notificationHubService.apns.send(null, '{"aps":{"alert":"Hello from Mobile Serivces!"}}',
function(error) {
if (!error) {
console.log("Notification successful");
}
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment