4

AndroidでPhonegap通知を使用する方法を自分自身で教えています。通知を表示することはかなり簡単なプロセスAndroid Phonegapアプリ - アクティビティを前面に表示する

public void triggerTestNotification(String tag, int id,Context ctxt) 
{ 
    Intent notificationIntent = new Intent(context, PallActivity.class); 
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
    Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    notificationIntent.setAction(Intent.ACTION_MAIN); 
    notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER); 

    PendingIntent contentIntent = PendingIntent.getActivity(ctxt, 0, 
    notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

    Notification not = new Notification.Builder(ctxt) 
    .setContentTitle("Title").setContentText("Title") 
    .setTicker("Tick tock") 
    .setAutoCancel(true) 
    .setContentIntent(contentIntent) 
    .setSmallIcon(ctxt.getApplicationInfo().icon).build(); 
    NotificationManager notificationManager = (NotificationManager)  
    ctxt.getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(tag, id, not); 
} 

ように見えながら、私はむしろ、より困難を発見したものを

  • 以下しているユーザーは、ユーザーが離れて行くと何かをやって起動するアプリ
  • を起動します他の - アプリがバックグラウンドにされ
  • 通知が
  • が表示されて到着した
  • ユーザーが通知をクリックします。

この時点で、アプリはフォアグラウンドに戻ってきます。私は自分の意図コードを考えました

public class PallActivity extends Activity 
{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
    super.onCreate(savedInstanceState); 
    finish(); 
    forceMainActivityReload(); 
} 

private void forceMainActivityReload() 
{ 
    PackageManager pm = getPackageManager(); 
    Intent launchIntent =  
    pm.getLaunchIntentForPackage(getApplicationContext().getPackageName());   
    startActivity(launchIntent); 
} 

@Override 
protected void onResume() 
{ 
    super.onResume(); 
    final NotificationManager notificationManager = (NotificationManager) 
    this.getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.cancelAll(); 
} 

} 

これはどうしても問題ありません。明らかに、私はここで何か間違っている - おそらくインテントの旗で。私は正しい軌道に乗ることができるかもしれない人に多くの義務を負うだろう。

+0

? –

+0

私は別のプラグインでそれを見たので、そこに 'finish()'を配置しました。しかし、私は 'onCreate'の最後に移動した' finish() 'を使って自分のコードを試してみました。アプリはまだフォアグラウンドに戻らない。 – DroidOS

+1

Pallの活動の目的は何ですか?あなたのアプリのランチャー活動にすぐにリダイレクトされます。 'PallActivity'はあなたのランチャー活動ではありませんか?あなたのマニフェストを含めることができますか? –

答えて

2

あなたのoncreateからfinish();を削除すると、どこにでも進む前にfinishが呼び出されます。

のPallActivityを開始する代わりに、と思われます。第2のアクティビティを開始する、または少なくともPallActivityを第2のアクティビティとして処理する予定です。 forceMainActivityReload();のようにそれを開始する必要はありません。より複雑で問題を混乱させるのではなく、通知の意図から始めたい活動を開始することになります。

notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR; 
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

使用している状況であることを確認します。コメントで述べたように、

Context context = getApplicationContext(); 

と通知からこれらを削除します。

notificationIntent.setAction(Intent.ACTION_MAIN); 
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER); 

をここでは詳細の過多があればです必要:

Resume an activity when clicked on a notification

あなたは `onCreate`で` finish`呼び出すなぜ

Android: How to resume an App from a Notification?

resuming an activity from a notification

Notification Resume Activity

Intent to resume a previously paused activity (Called from a Notification)

Android: resume app from previous position

How to make notification intent resume rather than making a new intent?

+1

ありがとう、イヴェット - あなたが提供した有用なリンクの範囲は特に評価されています。 – DroidOS

関連する問題