2016-08-04 32 views
2

私は特に経験豊富なAndroid開発者ではないので、事前にお詫びします。私はC#を使用してXamarinのAndroidプロジェクトを作成しています。私はこの質問が重複していないことを願っています。まだ見つけられないようですが、もしそうなら、そのようにマークしてください。私は喜んで質問を削除します。バックグラウンドAndroid Xamarinの場合、アプリをフォアグラウンドで表示するように通知する

ドローイングアプリのアイコンが起動時と実行中に通知バーに表示されます。アプリが破壊イベントに入ると、アイコンとメッセージが削除されます。これまでのところ、私はそれを持っているようですが、通知メッセージをクリックして実行中のアプリケーションをフォアグラウンドに持っていく方法を見つけたり、見つけたりすることができません。私はこれについて私のコードで間違った一般的な方向を取っていると思うし、多分これには差し迫った意図やそのようなものが含まれていますか?ここに私のコードです。たぶん私は近くにいるかもしれないし、間違った方向に向いているかもしれませんが、誰かが助けてくれるポインタや助けてくれれば幸いです。

[Activity(Label = "MyActivity", MainLauncher = true, Theme = "@android:style/Theme.NoTitleBar")] 
public class MainActivity : Activity 
{ 
    Notification notification = null; 
    NotificationManager notificationManager = null; 
    const int notificationId = 0; 
    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 
     Notification.Builder builder = new Notification.Builder(this) 
              .SetContentTitle("My App is Running") 
              .SetContentText("Show in forground") 
              .SetSmallIcon(Resource.Drawable.Icon); 

     // Build the notification: 
     notification = builder.Build(); 

     // Get the notification manager: 
     notificationManager = GetSystemService(Context.NotificationService) as NotificationManager; 

     // Publish the notification: 
     notificationManager.Notify(notificationId, notification); 
    } 

    protected override void OnDestroy() 
    { 
     Log.Debug(logTag, "Location app is becoming inactive"); 
     notificationManager.Cancel(notificationId); 
     base.OnDestroy(); 
    } 
} 

答えて

2

私が見つけたり 通知メッセージのクリックがフォアグラウンドに私の実行中のアプリを持って来るようにする方法を見つけ出すように見えることはできません(それ がまだない場合のみ)

あなたは、それがクリックされたときに何をする必要があるか(どのアクティビティを開始するか)通知する必要があります。

var intent = new Intent(context, typeof(MainActivity)); 
//activity will not be launched if it is already running at the top of the history stack. 
intent.AddFlags(ActivityFlags.SingleTop); 

//Flag indicating that this PendingIntent can be used only once. 
var pendingIntent = PendingIntent.GetActivity(context, 0 
              , intent, PendingIntentFlags.OneShot); 
Notification.Builder builder = new Notification.Builder(this) 
              .SetContentTitle("My App is Running") 
              .SetContentText("Show in forground") 
              .SetSmallIcon(Resource.Drawable.Icon) 
              .SetContentIntent(pendingIntent); 

意味し、他のどのような選択肢あなたが持っているものをそれぞれ上記のコードのすべての項目を知っているNotification.Builder詳細についてはこちらをご覧ください。

+0

これは私が思っていたことですが、私はよく慣れていない未決の意図を見ていました。しかし、あなたが与えたものが実際の答えであるように見えるので、本当にすぐにそれを試してみましょう。どうも。 – user192632

+0

完璧に動作する、あなたは賢明にもPendingIntentFlags.OneShotを使いました。どうも。私は答えとしてマークします。 – user192632

関連する問題