此代码从服务器获取请求并执行某些操作。我希望当应用程序关闭时获取请求并通过通知显示它。我可以从firebase获取消息并显示它,但这不是我想要的东西
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.messaging.FirebaseMessaging;
public class MyActivity extends ActionBarActivitiy implements AsyncTaskCompleteListener{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
.
.
.
}
private void displayFirebaseRegId() {
SharedPreferences pref = getApplicationContext().getSharedPreferences(Config.SHARED_PREF, 0);
String regId = preferenceHelper.getSessionToken();
if (!TextUtils.isEmpty(regId))
}
private void subscribeToPushService() {
FirebaseMessaging.getInstance().subscribeToTopic("news");
String token = FirebaseInstanceId.getInstance().getToken();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
@Override
protected void onDestroy() {
}
@Override
public void onTaskCompleted(String response, int service) {
super.onTaskCompleted(response, service);
switch (service) {
case MyValues.Service.REQUEST_NEW:
// addNotification();
...
if (requestId == MyValues.NO_REQUEST) {
addNotification();
addNewFragment(new myFragment(), false,
MyValues.REQUEST_, true);
} else {
...
}
break;
}
}
abstract public class ActionBarActivitiy extends AppCompatActivity implements OnClickListener , AsyncTaskCompleteListener
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
public void addNotification() {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(MyApplication.applicationContext)
.setSmallIcon(R.mipmap.ic_cl)
.setContentTitle("Notifications get request")
.setContentText("This is get request notification");
Intent notificationIntent = new Intent(MyApplication.applicationContext, MainActivity.class);
PendingIntent contentIntent =
PendingIntent.getActivity(MyApplication.applicationContext, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
builder.setSound(Uri.parse("android.resource://"
+ MyApplication.applicationContext.getPackageName() + "/" + R.raw.sfx_counter_loop));
// builder.setOngoing(true);
try {
builder.setWhen(System.currentTimeMillis());
builder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
builder.setLights(Color.RED, 3000, 3000);
}catch (Exception e){
}
// Add as notification
NotificationManager manager = (NotificationManager)
MyApplication.applicationContext.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
@Override
public void onTaskCompleted(String response, int service) {
}
....
}
public interface AsyncTaskCompleteListener
{
void onTaskCompleted(String response, int service);
}
答案 0 :(得分:0)
您可以使用A服务,警报管理器来执行此操作。
安排计时器将运行该服务。该服务将获取数据,然后显示通知。您也可以使用IntentService来释放主线程。
修改强>
以下是警报管理器的链接:
https://developer.android.com/training/scheduling/alarms.html
和IntentService:
https://developer.android.com/training/run-background-service/create-service.html
此外,要在您的应用关闭之前调用某项任务,请在调用super之前在onStop上调用您的方法。