android - How to push notifications with Google Cloud Messaging? -
i'm trying implement push notification in android app. understand, need sender , receiver installed on device.
i googled on gcm found need create project in google dev console. got project id , api key.
i found there third party applications (mixpanel) can send push notifications information above. can use google play send notifications app users?
on programming side, got permissions required in manifest.xml
file
<uses-permission android:name="android.permission.internet" /> <!-- gcm connects internet services. --> <uses-permission android:name="android.permission.internet" /> <!-- gcm requires google account. --> <uses-permission android:name="android.permission.get_accounts" /> <!-- keeps processor sleeping when message received. --> <uses-permission android:name="android.permission.wake_lock" /> <!-- creates custom permission app can receive messages. --> <permission android:name="com.androidexample.gcm.permission.c2d_message" android:protectionlevel="signature" /> <uses-permission android:name="com.androidexample.gcm.permission.c2d_message" /> <!-- app has permission register , receive data message. --> <uses-permission android:name="com.google.android.c2dm.permission.receive" /> <!-- network state permissions detect internet status --> <uses-permission android:name="android.permission.access_network_state" /> <!-- permission vibrate --> <uses-permission android:name="android.permission.vibrate" />
and broadcast receiver
<receiver android:name="com.google.android.gcm.gcmbroadcastreceiver" android:permission="com.google.android.c2dm.permission.send" > <intent-filter> <!-- receives actual messages. --> <action android:name="com.google.android.c2dm.intent.receive" /> <!-- receives registration id. --> <action android:name="com.google.android.c2dm.intent.registration" /> <category android:name="com.androidexample.gcm" /> </intent-filter> </receiver> <service android:name="com.androidexample.gcm.gcmintentservice" />
from here on, got confused "sender" , "receiver" parts.
do have implement gcmbroadcastreceiver
?
if want implement receiving notification part, how ?
any reference working code example good.
yes, have implement gcmbroadcastreceiver class catch messages gcm service. purpose of receiver start intent service if app off. here common implementation:
public class gcmbroadcastreceiver extends wakefulbroadcastreceiver { @override public void onreceive(context context, intent intent) { componentname comp = new componentname(context.getpackagename(), gcmintentservice.class.getname()); startwakefulservice(context, (intent.setcomponent(comp))); setresultcode(activity.result_ok); } }
the purpose of intent service doing caught gcm message:
public class gcmintentservice extends intentservice { @override protected void onhandleintent(intent intent) { // getting gcm message fields bundle extras = intent.getextras(); // doing message. // e.g. showing push notification } }
you can see e.g. this article details.
Comments
Post a Comment