How to get android notifications when app was clos

2019-01-16 21:34发布

问题:

From webserivce I am getting data in the form of dates and time, means for some particular date i am having some set of slots. The following diagram gives the detailed explanation.

Here on each date I have some set to timings. Here what i want is to display notification at the particular date and time. If the response from database consists of tomorrow date like 07/12/2012 at 11:00 AM. I need to display notification at that time.

I have some idea regarding notification manager and code i am using is..

Main.java

      NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_action_search, "A New Message!", System.currentTimeMillis());

    Intent notificationIntent = new Intent(this, Main.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    notification.setLatestEventInfo(Main.this, notificationTitle, notificationMessage, pendingIntent);
    notificationManager.notify(10001, notification);

But here i need to get notifications when app is closed also. So, anyone help me with this.

回答1:

Add a (started) Service to your application. Even though the user quitted your app, the service will still run in the background.

Furthermore you can implement a BroadcastReceiver that will listen to the phone's Intents and let your service be started when the phone boots !

MyService.java

public class MyService extends Service {

@Override
public int onStartCommand(Intent intent, int flags, int startId){
    // START YOUR TASKS
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
    // STOP YOUR TASKS
super.onDestroy();
}

@Override
public IBinder onBind(Intent intent){
    return null;
}

BootReceiver.java

public class BootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
            Intent serviceIntent = new Intent("your.package.MyService");
            context.startService(serviceIntent);
            }
        }
    }
}

AndroidManifest.xml

// in manifest tag

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

// in your application tag

<service android:name=".MyService">
    <intent-filter>
        <action android:name="your.package.MyService" />
    </intent-filter>
</service>

<receiver
    android:name=".BootReceiver"
    android:enabled="true"
    android:exported="true"
    android:label="BootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

If you want to start your service from an activity, just use

private boolean isMyServiceRunning() {
         ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
         for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
             if (MyService.class.getName().equals(service.service.getClassName())) {
                 return true;
             }
         }
         return false;
     }

if (!isMyServiceRunning()){
     Intent serviceIntent = new Intent("your.package.MyService");
     context.startService(serviceIntent);
}


回答2:

If data comes from your server then utilizing GCM might be good approach. In this case server will have ability to wake/start your application.

Creating a service which is constantly running in your case is a crappy solution. IMO better approach is use of AlarmManager. Alarm manager will invoke intent on specific time. (note that if phone reboots you have to register intent again).