在xamarin

时间:2018-12-16 21:16:22

标签: android xamarin notifications

我正在为Android开发Xamarin应用。有一个通知。点击后,出现黑屏几秒钟,所以我想添加一个初始屏幕。我正在尝试添加它,但是每次尝试都不成功。这是我的代码的一部分。

主要活动,我在其中创建信息页以进行通知。

[Activity(Theme = "@style/MainTheme", Icon = "@drawable/icon", LaunchMode = Android.Content.PM.LaunchMode.SingleTop)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    Bundle bundle;

    protected override void OnCreate(Bundle bundle)
    {
        this.bundle = bundle;
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(bundle);
        global::Xamarin.Forms.Forms.Init(this, bundle);
        Xamarin.Forms.Application nowa = new App();
        LoadApplication(nowa);

        Intent alarmIntent = new Intent(this, typeof(AlarmReceiver));

        if (PendingIntent.GetBroadcast(this, 0, alarmIntent, PendingIntentFlags.NoCreate) == null)
        {
            PendingIntent pending = PendingIntent.GetBroadcast(this, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
            AlarmManager alarmManager = GetSystemService(AlarmService).JavaCast<AlarmManager>();

            alarmManager.SetRepeating(AlarmType.RtcWakeup, BootReceiver.FirstReminder(), BootReceiver.reminderInterval, pending);
            PendingIntent pendingIntent = PendingIntent.GetBroadcast(this, 0, alarmIntent, 0);
            App.Current.Properties["alarmDefined"] = "true";
        }
    }

    protected override void OnNewIntent(Intent intent)
    {
        base.OnNewIntent(intent);
        Intent = intent;
        ShowInfoPage();
    }

    public void ShowInfoPage()
    {
        Xamarin.Forms.Application.Current.MainPage.Navigation.PushAsync(new InfoPage());
    }

}

用于通知的广播接收器。

[BroadcastReceiver]
class AlarmReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        var title = "Title";
        var message = "Message";

        Intent backIntent = new Intent(context, typeof(MainActivity));
        backIntent.SetFlags(ActivityFlags.NewTask);

        var resultIntent = new Intent(context, typeof(InfoActivity));

        PendingIntent pending = PendingIntent.GetActivities(context, 0,
            new Intent[] { backIntent, resultIntent },
            PendingIntentFlags.OneShot);

        var builder =
            new Notification.Builder(context)
                .SetContentTitle(title)
                .SetContentText(message)
                .SetAutoCancel(true)
                .SetSmallIcon(Resource.Drawable.ic_launcher)
                .SetDefaults(NotificationDefaults.All);

        builder.SetContentIntent(pending);
        var notification = builder.Build();
        var manager = NotificationManager.FromContext(context);
        manager.Notify(1331, notification);

    }
}

用于通知的InfoActivity:

[Activity(LaunchMode = Android.Content.PM.LaunchMode.SingleTop)]
public class InfoActivity : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Intent i = new Intent(this, typeof(MainActivity));
        i.AddFlags(ActivityFlags.ReorderToFront);
        this.StartActivity(i);
        Finish();
    }
}

SplashActivity,它是在打开应用程序后创建的,但是我想在点击通知后使用类似的功能。

[Activity(Theme = "@style/MyTheme.Splash", MainLauncher = true, NoHistory = true)]
public class SplashActivity : AppCompatActivity
{
    public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState)
    {
        base.OnCreate(savedInstanceState, persistentState);
        Log.Debug(typeof(SplashActivity).Name, "SplashActivity.OnCreate");
    }

    protected override void OnResume()
    {
        base.OnResume();
        Task startupWork = new Task(() => { Startup(); });
        startupWork.Start();
    }

    public override void OnBackPressed() { }

    void Startup()
    {
        StartActivity(new Intent(Application.Context, typeof(MainActivity)));
    }
}

到目前为止,我尝试在BroadcastReceiver中创建飞溅活动的意图(并在MainActivity中删除),但随后未调用InfoActivity。任何有关该主题的建议将不胜感激。

0 个答案:

没有答案