2017-03-08 1 views
1

このコードを使用して、通知バーに通知を表示しています。通知がタップされると、メインアクティビティが開始されます。 MvvmCrossを使ったXamarinフォームアプリケーションではなく、ビューモデルを起動することは可能ですか?Xamarinは通知タップで開いたビューモデルを作成します

Intent notificationIntent = new Intent(context,typeof(MainActivity)); 
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |        Intent.FLAG_ACTIVITY_SINGLE_TOP);  
    PendingIntent pIntent = PendingIntent.getActivity(context, code, 
      notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    NotificationManager manager = (NotificationManager) context 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
    NotificationCompat.Builder notify = new NotificationCompat.Builder(
      context); 

    notify.setContentIntent(pIntent); 
    notify.setSmallIcon(R.drawable.app_icon); 
    notify.setContentTitle(“Title”); 
    manager.notify(reqCode, notify.build()); 

答えて

0

私の考えはMessagingCenterと組み合わせてPutExtraを利用するようにしました。

まず、あなたは通知バーに通知を示しています。MainActivity.csで

Intent intent = new Intent(Forms.Context, typeof(MainActivity)); 
if (openPage) 
{ 
    intent.SetFlags(ActivityFlags.SingleTop); 
    intent.PutExtra("OpenPage", "SomePage"); 
} 

const int pendingIntentId = 0; 
PendingIntent pendingIntent = PendingIntent.GetActivity(Forms.Context, pendingIntentId, intent, PendingIntentFlags.OneShot); 

var nMgr = (NotificationManager)Android.App.Application.Context.GetSystemService(Context.NotificationService); 
Notification.Builder notBuilder = new Notification.Builder(Android.App.Application.Context) 
    .SetContentIntent(pendingIntent) 
    .SetContentTitle("SomeApp") 
    .SetContentText(message) 
    .SetDefaults(NotificationDefaults.Sound | NotificationDefaults.Vibrate) 
    .SetSmallIcon(Resource.Drawable.ic_launcher) 
    .SetAutoCancel(true); 

var notification = notBuilder.Build(); 
nMgr.Notify(0, notification); 

あなたは余分なコンテンツをチェック:

protected override void OnNewIntent(Intent intent) 
{ 
    // Send message to the PCL (XF) if a certain page should be opened. 
    if (intent.HasExtra("OpenPage")) 
    { 
     string pageName = intent.GetStringExtra("OpenPage") ?? "None"; 

     if (pageName != "None") 
     { 
      var message = new OpenPageMessage { PageName = pageName }; 
      MessagingCenter.Send(message, Message.Msg_OpenPage); 
     } 
    } 

    base.OnNewIntent(intent); 
} 

そして、あなたの中央ナビゲーションインスタンス(例えばMainPage )、このメッセージを購読する:

MessagingCenter.Subscribe<Message.OpenPageMessage>(this, Message.Msg_OpenPage, (async) message => 
{ 
    // Loads a certain page if a message is received 

    switch (message.PageName) 
    { 
     case "SomePage": 
      await Navigation.PushModalAsync(new SomePage(), true); 
      break; 
     default: 
      break; 
    } 
}); 
この sourceの助けを借りて

public class Message 
{ 
    public const string Msg_OpenPage = "OpenPage"; 

    public class OpenPageMessage { 
     public string PageName { get; set; } 
    } 
} 

はさらに、ここで私のMessage.csです。

編集

一度に複数のプッシュ通知を持っている場合はどこnotficationsどこ上書きの問題があります。異なるrequestCodeを使用するか、FLAG_UPDATE_CURRENTを使用することができます。

+0

こんにちは、ユーザーがプッシュ通知をクリックしている間に特定のページに移動するのに「余分なものを」使用しましたが、いくつかの状態は失敗します。私は2種類の通知、すなわち1.注文2.レビューを持っています。オーダータイプの通知には、追加するオーダーを追加し、レビュータイプの通知を受け取ったときに、追加するレビューを追加しましたが、オーダータイプの通知をクリックすると、「レビュー」エクストラが表示されます。最後の通知タイプが保存されます。どのようにそれを修正することができます。 – Deepak

+0

私は、新しい質問を作成することをお勧めします。ここでは、(コードなどで)わかりやすく説明し、必要なものと問題の場所を記述します。通知タイプは1つしか使用していないため、現在問題を再現することはできません。 – testing

+0

よろしくお願いいたします。 – Deepak

関連する問題