2

これは長いポストになるでしょう! (コーヒー/ポップコーンのカップをつかむ)Xamarin.android通知onClickは新しいアクティビティにはかかりません

私はAltBeacon私のコードのビーコンを表示するには、Xamarinのサンプルを使用しています。

私はXamarinで通知を作成する例としてthisを見つけました。

コアロジックが進むアプリケーションクラスがあります。

public class AltBeaconSampleApplication : Application, IBootstrapNotifier 
{ 
    private const string TAG = "AltBeaconSampleApplication"; 

    BeaconManager _beaconManager; 

    private RegionBootstrap regionBootstrap; 
    private Region _backgroundRegion; 
    private BackgroundPowerSaver backgroundPowerSaver; 
    private bool haveDetectedBeaconsSinceBoot = false; 

    private string nearbyMessageString = "A beacon is nearby."; 
    private string nearbyTitleString = "AltBeacon Reference Application"; 

    private MainActivity mainActivity = null; 
    public MainActivity MainActivity 
    { 
     get { return mainActivity; } 
     set { mainActivity = value; } 
    } 

    private NotificationActivity notificationActivity = null; 

    public NotificationActivity NotificationActivity 
    { 
     get { return notificationActivity; } 
     set { notificationActivity = value; } 
    } 

    public AltBeaconSampleApplication() : base() { } 
    public AltBeaconSampleApplication(IntPtr javaReference, Android.Runtime.JniHandleOwnership transfer) : base(javaReference, transfer) { } 


    public override void OnCreate() 
    { 
     base.OnCreate(); 

     _beaconManager = BeaconManager.GetInstanceForApplication(this); 

     var iBeaconParser = new BeaconParser(); 
     // Estimote > 2013 
     iBeaconParser.SetBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"); 
     _beaconManager.BeaconParsers.Add(iBeaconParser); 

     Log.Debug(TAG, "setting up background monitoring for beacons and power saving"); 
     // wake up the app when a beacon is seen 
     _backgroundRegion = new Region("backgroundRegion", null, null, null); 
     regionBootstrap = new RegionBootstrap(this, _backgroundRegion); 

     // simply constructing this class and holding a reference to it in your custom Application 
     // class will automatically cause the BeaconLibrary to save battery whenever the application 
     // is not visible. This reduces bluetooth power usage by about 60% 
     backgroundPowerSaver = new BackgroundPowerSaver(this); 

     PerformHttpRequest(); 
    } 

    public void DidDetermineStateForRegion(int state, AltBeaconOrg.BoundBeacon.Region region) 
    { 
    } 

    public async void PerformHttpRequest() 
    { 
     try 
     { 
      using (var client = new HttpClient()) 
      { 
       var uri = "http://exampleuri"; 
       var result = await client.GetStringAsync(uri); 
       var response = JsonConvert.DeserializeObject<BeaconURL>(result); 
       SendNotificationFromBeacon(response); 

      } 
     } 
     catch(Exception ex) 
     { 
      throw ex; 
     }    
    } 

    private void SendNotificationFromBeacon(BeaconURL receivedNotification) 
    { 
     // Setup an intent for SecondActivity: 
     Intent notificationIntent = new Intent(this, typeof(NotificationActivity)); 

     // Pass some information to SecondActivity: 
     notificationIntent.PutExtra("CompaignUrl", receivedNotification.CompaignUrl); 
     notificationIntent.PutExtra("MediaUrl", receivedNotification.MediaUrl); 
     notificationIntent.PutExtra("titleText", receivedNotification.Title); 
     notificationIntent.SetFlags(ActivityFlags.NewTask); 

     // Create a task stack builder to manage the back stack: 
     Android.App.TaskStackBuilder stackBuilder = Android.App.TaskStackBuilder.Create(this); 

     // Add all parents of SecondActivity to the stack: 
     stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(NotificationActivity))); 

     // Push the intent that starts SecondActivity onto the stack: 
     stackBuilder.AddNextIntent(notificationIntent); 

     // Obtain the PendingIntent for launching the task constructed by 
     // stackbuilder. The pending intent can be used only once (one shot): 
     const int pendingIntentId = 0; 
     PendingIntent pendingIntent = 
      stackBuilder.GetPendingIntent(pendingIntentId, PendingIntentFlags.OneShot); 

     // Instantiate the builder and set notification elements, including 
     // the pending intent: 
     var builder = 
      new NotificationCompat.Builder(this) 
       .SetContentTitle(receivedNotification.Title) 
       .SetContentText(receivedNotification.Text) 
       .SetSmallIcon(Android.Resource.Drawable.IcDialogInfo); 

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

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

     // Publish the notification: 
     const int notificationId = 0; 
     notificationManager.Notify(notificationId, notification); 
    } 
} 

BeaconURLPOCOクラス NotificationActivityは基本活動クラスです。

私はHttpClient要求を実行し、データを取得します。私は通知を作成し、それを私の画面上に提示する。これは次のようになります enter image description here

通知をタップすると、NotificationActivityに移動しません。私は、ApplicationClassからアクティビティを呼び出そうとしています。このようなものを実行するための正しい方法ですか?親切に詳細を提供します。

ありがとうございました。

編集

var builder = 
    new NotificationCompat.Builder(this) 
     .SetContentTitle("receivedNotification.Title") 
     .SetContentText("receivedNotification.Text") 
     .SetSmallIcon(Android.Resource.Drawable.IcDialogInfo) 
     .SetContentIntent(pendingIntent); 

:追加NotificationActivityクラスが

[Activity(Label = "NotificationActivity")] 
public class NotificationActivity : MainActivity 
{ 
    protected override void OnCreate(Bundle savedInstanceState) 
    { 
     base.OnCreate(savedInstanceState); 

     // Create your application here 
     SetContentView(Resource.Layout.NotificationLayout); 

     TextView titleTextView = FindViewById<TextView>(Resource.Id.txtTitle); 
     titleTextView.Text = Intent.Extras.GetString("titleText", ""); 

     ImageView mediaImage = FindViewById<ImageView>(Resource.Id.imgViewMedia); 
     mediaImage.SetImageBitmap(GetImageBitmapFromUrl(Intent.Extras.GetString("MediaUrl", ""))); 
    } 

    private Bitmap GetImageBitmapFromUrl(string url) 
    { 
     Bitmap imageBitmap = null; 

     using (var webClient = new WebClient()) 
     { 
      var imageBytes = webClient.DownloadData(url); 
      if (imageBytes != null && imageBytes.Length > 0) 
      { 
       imageBitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length); 
      } 
     } 

     return imageBitmap; 
    } 
} 
+0

'NotificationActivity'アクティビティの' Activity'属性( 'Activity'属性に' Name'を手動で割り当てる場合はマニフェストの正しいエンティティ)に 'ParentActivity'名を設定していますか? – SushiHangover

+0

NotificationActivityクラスはActivityのサブクラスです。私は同じクラスのコードを追加しました。あなたは親切にそれを@SushiHangoverで調べることができますか?ありがとう – Xander

+0

私は、クラスそのものではなく、そのクラスの '[Activity(....)]属性について話していました。 – SushiHangover

答えて

1

あなたがする必要がある最初の事は、通知ビルダー内の保留中の意図を設定することで、それはあなたのNotificationActivity起動を取得しますもう1つは投稿したものからバックスタック設定を取得することです。バックボタンを使用した場合、ユーザーがアプリを終了するときのフローを確認できません。

あなたはそのプレス戻るボタンは、その後、あなたはすなわち、あなたのNotificationActivityアクティビティ属性にParentActivityを追加することができたときに、ユーザーがMainActivityに戻りたい場合は、次の

[Activity(Label = "NotificationActivity", ParentActivity = typeof(MainActivity))] 

そしてこうしてライン:

stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(NotificationActivity))); 

MainActivityをバックスタックに追加しますか。

+0

この欠けている部分 SetContentIntent(pendingIntent)がトリックをしました。驚くばかり! – Xander

関連する問題