Last active
December 14, 2015 05:29
-
-
Save ChrisRisner/5036154 to your computer and use it in GitHub Desktop.
Android Push
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 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); | |
} | |
}); | |
}); | |
} | |
}); | |
} |
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
import android.app.NotificationManager; | |
import android.support.v4.app.NotificationCompat; |
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 GCMIntentService(){ | |
super(ToDoActivity.SENDER_ID); | |
} |
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
@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()); | |
} |
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
@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(); | |
} | |
} |
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
<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" /> |
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 String SENDER_ID = "794994939600"; |
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="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" /> |
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
GCMRegistrar.checkDevice(this); | |
GCMRegistrar.checkManifest(this); | |
if (GCMRegistrar.getRegistrationId(this).equals("")) { | |
GCMRegistrar.register(this, SENDER_ID); | |
} |
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 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