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();
}
}
これに対処しますが、何もしません。明らかに、ここで何か問題が発生しています。おそらくインテント フラグです。私を正しい軌道に乗せることができる人には、とても感謝しています