Last active
December 21, 2015 14:48
-
-
Save ChrisRisner/6321731 to your computer and use it in GitHub Desktop.
NotificationHubs
This file contains hidden or 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
gcm = GoogleCloudMessaging.getInstance(this); | |
String connectionString = "<Notification Hub Listen Access Connection String>"; | |
hub = new NotificationHub("<Notification Hub Name>", connectionString, this); | |
registerWithNotificationHubs(); |
This file contains hidden or 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-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"/> |
This file contains hidden or 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=".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> |
This file contains hidden or 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 String SENDER_ID = "<Project ID from Google API Console>"; | |
private GoogleCloudMessaging gcm; | |
private NotificationHub hub; |
This file contains hidden or 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 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()); | |
} |
This file contains hidden or 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 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); | |
} |
This file contains hidden or 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
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: | |
UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | | |
UIRemoteNotificationTypeSound]; |
This file contains hidden or 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
- (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]; | |
} |
This file contains hidden or 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
- (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); | |
} | |
}]; | |
} |
This file contains hidden or 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
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