Skip to content

Instantly share code, notes, and snippets.

@ChrisRisner
Last active December 14, 2015 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisRisner/5036154 to your computer and use it in GitHub Desktop.
Save ChrisRisner/5036154 to your computer and use it in GitHub Desktop.
Android Push
function insert(item, user, request) {
request.execute({
success: function(){
request.respond();
sendNotifications(item);
},
error: function(err){
request.respond(500, "Error");
}
});
}
function sendNotifications(item){
var registrationTable = tables.getTable('Registration');
registrationTable.read({
success: function(registrations){
registrations.forEach(function(registration){
push.gcm.send(registration.registrationId, item.text, {
success: function(response) {
console.log('Push notification sent: ', response);
}, error: function(error) {
console.log('Error sending push notification: ', error);
}
});
});
}
});
}
import android.app.NotificationManager;
import android.support.v4.app.NotificationCompat;
public GCMIntentService(){
super(ToDoActivity.SENDER_ID);
}
@Override
protected void onMessage(Context context, Intent intent) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Todo Created!")
.setPriority(Notification.PRIORITY_HIGH)
.setContentText(intent.getStringExtra("message"));
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
@Override
protected void onRegistered(Context context, String registrationId) {
MobileServiceClient client;
try {
client = new MobileServiceClient(
"https://<YourMobileServiceUrl>.azure-mobile.net/",
"<YourApplicationKey>", context);
MobileServiceTable<Registration> registrationTable = client.getTable(Registration.class);
Registration registration = new Registration();
registration.setRegistrationId(registrationId);
registrationTable.insert(registration, new TableOperationCallback<Registration>() {
@Override
public void onCompleted(Registration entity, Exception exception,
ServiceFilterResponse response) {
if (exception != null) {
Log.e("GCMIntentService", "Exception - " + exception.getMessage());
} else {
Log.i("GCMIntentService", "Success");
}
}
});
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
<permission android:name="com.example.chrisandroid.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.chrisandroid.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
public static final String SENDER_ID = "794994939600";
<receiver android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.exmaple.chrisandroid" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
if (GCMRegistrar.getRegistrationId(this).equals("")) {
GCMRegistrar.register(this, SENDER_ID);
}
public class Registration {
@com.google.gson.annotations.SerializedName("id")
private int mId;
@com.google.gson.annotations.SerializedName("registrationId")
private String mRegistrationId;
public int getId() { return mId; }
public final void setId(int id) { mId = id; }
public String getRegistrationId() { return mRegistrationId; }
public final void setRegistrationId(String registrationId) { mRegistrationId = registrationId; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment