2017-03-27 3 views
0

このコードはサーバからリクエストを取得して何かをします。私は、アプリケーションが閉じられたときにリクエストを受け取り、通知で表示したい。私はfirebaseからメッセージを取得し、それを示すが、これは私はあなたが使用することができますアプリが閉じられたとき、またはバックグラウンドでrunnigしたときに、このコードからonTaskCompleteの通知を表示する方法

MyActivity

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; 
} 
} 

ActionBarActivitiy

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) { 

} 

.... 

} 

インタフェースAsyncTaskCompleteListener

public interface AsyncTaskCompleteListener 
{ 
void onTaskCompleted(String response, int service); 
} 

答えて

0

をしたいものではありませんすることができますサービス、そうするためのアラームマネージャー。

サービスを実行するタイマーをスケジュールします。サービスはデータを取得し、通知を表示します。 IntentServiceを使ってメインスレッドを解放することもできます。

https://developer.android.com/training/scheduling/alarms.html 

とIntentService:ここ

編集

は、アラームマネージャのためのリンクは、あなたのアプリが近づく前に特定のタスクを呼び出すために、また

https://developer.android.com/training/run-background-service/create-service.html 

、あなたを呼び出しますsuperを呼び出す前にonStopのメソッドを呼び出してください。

+0

tnx ...しかし、私はこれを行う方法を知らない... –

+0

あなたは私にそれを学ぶリンクを得ることができますか? –

+0

私はアプリが実行されていないときに回答 –

関連する問題