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.
Asked
Active
Viewed 4,854 times
2
-
can I declare the service anywhere in the project to get notify .. – hitesh Jan 27 '17 at 11:21
-
thanks now i understand – hitesh Jan 27 '17 at 11:27
-
You are welcome! Please upvote if it helped you! – Karthik CP Jan 27 '17 at 11:28
-
answer first comments cannot be upvoted – hitesh Jan 27 '17 at 11:52
-
Updated the answer! Happy coding – Karthik CP Jan 27 '17 at 11:58
-
you please upvote my question.. – hitesh Jan 27 '17 at 12:13
2 Answers
0
@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" />

Prashant Patil
- 11
- 1
- 5
0
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.

Karthik CP
- 1,150
- 13
- 24