如何在应用未运行时显示通知

时间:2016-09-29 16:51:21

标签: android

我的情景是向我的用户显示问候通知,即使他们没有使用该应用。如果打开或最小化打开,下面的代码工作正常。但是,我想在早上显示通知,即使用户没有打开该应用程序。

主要活动:

public class MainActivity extends Activity {

    private PendingIntent pendingIntent;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 18);
        calendar.set(Calendar.MINUTE, 40);
        calendar.set(Calendar.SECOND, 0);
        Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);

        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);

    } //end onCreate
}

我的接收器类:

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent)
    {
        /*Intent service1 = new Intent(context, MyAlarmService.class);
         context.startService(service1);*/
        try{
            Utils.generateNotification(context);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

}

Utils Class:

public class Utils {

    public static NotificationManager mManager;

    @SuppressWarnings("static-access")
    public static void generateNotification(Context context){ 

        mManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        Intent intent1 = new Intent(context,MainActivity.class);
        Notification notification = new Notification(R.drawable.ic_launcher,"This is a test message!", System.currentTimeMillis());
        intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingNotificationIntent = PendingIntent.getActivity(context,0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.setLatestEventInfo(context, "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent);
        mManager.notify(0, notification);
    }
}

我的警报服务:

public class MyAlarmService extends Service {

    @Override
    public IBinder onBind(Intent arg0)
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() 
    {
        // TODO Auto-generated method stub  
        super.onCreate();
    }

    @Override
    public void onStart(Intent intent, int startId)
    {
        super.onStart(intent, startId);
    }

    @Override
    public void onDestroy() 
    {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

}

下一个活动:

public class NextActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }   
}

1 个答案:

答案 0 :(得分:0)

你需要做的一些事情。如果用户强制关闭应用程序,您需要确保使用START_STICKY启动服务,以便在应用程序强制关闭时重新启动服务。

为了让用户在重新启动手机时启动服务,您需要$bookingForm = $('#bookingForm'); $bookingForm.submit(function(e) { e.preventDefault(); $.ajax({ url: '//formspree.io/your@email.com', method: 'POST', data: { client: $scope.client.name, email: $scope.client.email, phone: $scope.client.phone, // some other info }, dataType: 'json', }); }); receive BOOT_COMPLETED,然后才会启动该服务。

然后,您的服务将按正常方式设置警报。您需要存储要在某处设置的警报时间。我使用SharedPrefs(BroadcastReceiver),但我确信有更好的方法。