I'm trying to follow this guide.
My GCM app receives messages via a class that extends WakefulBroadcastReciver. Upon receipt of a gcm message, I execute a GcmIntentService that processes the message via the following code . . .
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
GlobalStuff.GCT = context.getApplicationContext();
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
My AndroidManifest.xml defines the service as follows . . .
<receiver
android:name="com.deanblakely.myapp.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.deanblakely.myapp" />
</intent-filter>
</receiver>
The guide however only discusses migration of InstanceIDListenerService, GcmListenerService, and GcmPubSub. There is no discussion of what to do if I am using a GcmBroadcastReceiver. What should I do? Is there a better migration guide somewhere?
Related
I am trying to get the Android BroadcastReceiver to run when a Firebase Cloud message notification is received by the Android system.
public class MyBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "MyBroadcastReceiver";
#Override
public void onReceive(final Context context, Intent intent) {
Toast.makeText(context, "EVENT OCCURED", Toast.LENGTH_LONG).show();
}
}
It is required in the AndroidManifest to specify receiver tags as such:
<receiver
android:name=".MyBroadcastReceiver" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</receiver>
As you can see in the Manifest above I've added:
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
to make sure the BroadcastReceiver fires when I plug cable to Android device. It works fine.
Therefore the issue lies with the:
<action android:name="com.google.firebase.MESSAGING_EVENT" />
Is this intent-filter action not recognized by the BroadcastReceiver?
Is there another intent-filter action for Firebase messaging the BroadcastReceiver uses?
The Android Intent messaging system processes Intents in three "channels": Activities, Services and BroadcastReceivers. The methods for publishing an Intent indicate the channel type: startActivity(), startService() and sendBroadcast(). An intent published with startService() will only match the intent filter of a Service. It is not tested against Activities and BroadcastReceivers.
Because action com.google.firebase.MESSAGING_EVENT is normally received by a Service derived from FirebaseMessagingService, it must be the case that the action is published in the Service channel. You will not be able to receive it with a BroadcastReceiver.
how to set a notification even if the app is not running or closed. I am writing a code in which I am checking for the new data updated in RESTful web services. and if new data is updated it will notify the app user that there is the new item. just like a mail arrived and the user gets notified. I am new to this please suggest.
Use services for background checking and use notification in services so that notification fire even app is closed.
This may help:
http://android-er.blogspot.in/2011/04/start-service-to-send-notification.html
You can create a service class in your package and remember to add it in your manifest. Start the service when your main activity starts or use broadcast reciever and start activity inside broadcast receiver.
#HITESH try this
Main.java
public class BroadcastReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(),
Intent.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
Android Manifest file
<receiver
android:name=".BroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.google.android.gcm.demo.app" />
</intent-filter>
</receiver>
<service android:name=".Intent" />
I am developing an Application which is using Push Notification (GCM) ,This application is receiving Push Noifications when app is running ,but when I forced stopped it from Settings ->apps->MyApp(click on it) and click on force stop ,then it is not receiving Push Notifications.
I have tested same with WhatsApp it is receiving Push Notifications when I force stop it .
So how can I implement same with my application .
Note : In code I am receiving PushNotifications in a sub class of WakefulBroadcastReceiver ,I have registered it statically in manifest even it is not called when application force stops.
public class GCM_Receiver extends WakefulBroadcastReceiver {
//Processes Gcm message .
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "GCM_Receiver", Toast.LENGTH_LONG).show();
ComponentName comp = new ComponentName(context.getPackageName(),GCMIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
Edit :
I have registered GCM_Receiver statically in this way :
<receiver
android:name="com.myApp.GCM_Receiver"
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.RETRY" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.myApp" />
</intent-filter>
</receiver>
Edit :
And my GCMIntentService code is below :
public class GCMIntentService extends IntentService{
//Constructor with super().
public GCMIntentService () {
super("GcmIntentService");
}
//Processes gcm messages .
#Override
protected void onHandleIntent(Intent intent) {
Log.d("GCMIntentService ", "GCMIntentService Started");
Toast.makeText(getApplicationContext(), "GCMIntentService Started", Toast.LENGTH_LONG).show();
GCM_Receiver.completeWakefulIntent(intent);
}}
Only Google Play services, through the com.google.android.c2dm.permission.SEND permission, can invoke that particular broadcast receiver. Since I assume that Play services does not include the FLAG_INCLUDE_STOPPED_PACKAGES flag when sending the broadcast, your force-stopped application will not receive the messages.
Interestingly, I don't receive any WhatsApp messages after force-stopping the app. Regardless of the app though, if it can receive messages while force stopped, then it definitely receives broadcasts from an intent with the FLAG_INCLUDE_STOPPED_PACKAGES flag.
I am learning GCM(Google cloud Messaging)
I Completed Following steps
1 - Activated and Obtained a Key for browser apps
2 - added following in manifest
Uses permission : com.google.android.c2dm.permission.RECEIVE
Created permission to prevent other Android applications from registering and receiving the Android application's messages
Created a receiver for com.google.android.c2dm.intent.RECEIVE, with the category set as applicationPackage. The receiver should require the com.google.android.c2dm.SEND permission
3 – in receiver
#Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(),
GcmMessageHandler.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
GcmMessageHandler it’s a Intend Service to show a notification on receive gcm message.following is onHandleIntent Method in GcmMessageHandler.
#Override
protected void onHandleIntent(Intent intent) {
handler.post(new Runnable() {
#Override
public void run() {
NotificationManager manager = (NotificationManager) getApplicationContext()
.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
getApplicationContext());
mBuilder.setSmallIcon(R.drawable.ic_launcher);
mBuilder.setContentTitle("GCM Received");
mBuilder.setContentText("GCM Message");
manager.notify(1, mBuilder.build());
}
});
Log.i("app", "Received ");
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
4 - MainActivity-to register and get clint ID, from log cat I copyed that id.
5 - from browser I make post request to https://android.googleapis.com/gcm/send with authorization,content-type,and registration_ids.
Its gets success message.
But my device not showing notification.
Sorry everyone,
Actually my program is correct. I think it’s a problem with my phone.
(I can’t receive messages from Facebook messenger also,)
but Today morning I surprised, I got every messages from my test app(message that I send 2 days ago), Facebook ,etc.
Thank you everyone.
Follow this and this easy tutorial to send and receive push nothification in Android.
And check package naming and AndroidManifest.xml file configured correctly as explained in this example and set receiver like this:
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="your package name here" />
</intent-filter>
</receiver>
I'm developing an Android application, i have a service running in background that i want to lauch it by introducing a custom ussd number. For example when i call #12345# my service starts.Thank you in advance
You can register a BroadcastReceiver in your manifest that listens for android.provider.Telephony.SECRET_CODE intents. You also specify the secret code in the manifest.
For example, this manifest entry registers MyBroadcastReceiver for the secret code *#*#12345#*#*.
<receiver android:name="MyBroadcastReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE"/>
<data android:scheme="android_secret_code" android:host="12345"/>
</intent-filter>
</receiver>
MyBroadcastReceiver should look something like this:
public class MyBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
// Create an intent to start your Service.
}
}