2

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.

hitesh
  • 13
  • 1
  • 6

2 Answers2

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