Receive push Noification when application has been force stopped .(Android) - android

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.

Related

Converting GCM apps to Firebase issue

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?

What Firebase-cloud-messaging Intent-filter to use for a BroadcastReceiver?

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 app is not running or closed

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" />

GCM not Working

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>

Broadcast intents not received by a service

I have an Android service which sends broadcast intents. I'm trying to get those intents in another application, which is an Android service. I wrote this in my manifest:
<!-- Service -->
<service android:enabled="true" android:name="...MyService"></service>
<!-- Receiver -->
<receiver android:name="...MyReceiver">
<intent-filter>
<action android:name="..."></action>
<action android:name="..."></action>
</intent-filter>
</receiver>
and this in my MyReceiver class:
public class ScannerBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// Process action.
Log.d(Globals.LOG_TAG, "Intent received.");
...
Unfortunately I never get the onReceive method invoked. Any idea why?
I start this service from another test application, so this is set as an Android library. The service is correctly started but this receiver is receiving nothing. Any idea what I'm doing wrong?
Thanks!
In manifest must be: android:name=".[package].ScannerBroadcastReceiver"
I solved the problem and I suppose it is due to the fact that this project was set as a library. If I don't set it this way the intent is correctly received. I haven't read about this anywhere.

Categories

Resources