- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用库 https://github.com/aritchie/notifications我可以正确地创建和安排通知。
我希望在 Android 中处理它们,以便根据通知 - 当用户点击它时它将导航到特定页面。
我发现当我点击通知(在我的 Android 项目中)时会触发以下事件
protected override void OnNewIntent(Intent intent)
{
}
但是,我在通知中找不到意图中的任何信息,以便建立到特定页面的导航。
如有任何建议,我们将不胜感激。
干杯!
编辑 #1(为相关问题添加其他代码):
如果我发出通知,并在收到通知之前关闭应用程序 - 我会收到一条错误消息,指出应用程序已崩溃。如果我收到通知并关闭应用程序 - 我可以从通知加载应用程序。
我有一个依赖服务,它使用以下方法。
public void Remind(DateTime dateTime, string msgtype, string usermedid)
{
DateTime now = DateTime.Now;
var diffinseconds = (dateTime - now).TotalSeconds;
Intent alarmIntent = new Intent(Forms.Context, typeof(AlarmBroadcastReceiver));
alarmIntent.PutExtra("notificationtype", msgtype);
alarmIntent.PutExtra("id", id);
PendingIntent pendingIntent = PendingIntent.GetBroadcast(Forms.Context, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
AlarmManager alarmManager = (AlarmManager)Forms.Context.GetSystemService(Context.AlarmService);
//TODO: For demo set after 5 seconds.
alarmManager.Set(AlarmType.ElapsedRealtime, SystemClock.ElapsedRealtime() + diffinseconds * 1000, pendingIntent);
}
[BroadcastReceiver(Enabled = true)]
[IntentFilter(new string[]{"android.intent.action.BOOT_COMPLETED"}, Priority = (int) IntentFilterPriority.LowPriority)]
public class AlarmBroadcastReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
string notificationtype = intent.GetStringExtra("notificationtype");
PowerManager.WakeLock sWakeLock;
var pm = PowerManager.FromContext(context);
sWakeLock = pm.NewWakeLock(WakeLockFlags.Partial, "GCM Broadcast Reciever Tag");
sWakeLock.Acquire();
intent = new Intent(Forms.Context, typeof(MainActivity));
intent.PutExtra("notificationtype", notificationtype);
intent.AddFlags(ActivityFlags.IncludeStoppedPackages);
// Instantiate the builder and set notification elements, including pending intent:
NotificationCompat.Builder builder = new NotificationCompat.Builder(Forms.Context)
.SetDefaults((int)NotificationDefaults.Sound | (int)NotificationDefaults.Vibrate)
.SetAutoCancel(true)
.SetContentIntent(PendingIntent.GetActivity(Forms.Context, 0, intent, 0)).SetContentTitle("Sample Notification")
.SetContentText("Hello World! This is my first action notification!")
.SetTicker("New Notification")
.SetSmallIcon(Resource.Drawable.icon);
// Build the notification:
Android.App.Notification notification = builder.Build();
notification.Flags = NotificationFlags.AutoCancel;
// Get the notification manager:
//NotificationManager notificationManager = Forms.Context.GetSystemService(Context.NotificationService) as NotificationManager;
var manager = NotificationManagerCompat.From(context);
// Publish the notification:
const int notificationId = 0;
manager.Notify(notificationId, notification);
sWakeLock.Release();
}
}
当应用程序关闭时,如何使广播接收器保持事件状态?
最佳答案
好吧,我花了一些时间才弄清楚这个问题。当应用程序位于后台并且单击通知时,将调用 OnNewIntent
。每次应用程序最小化和恢复时也会调用它...因此为了区分这两个事件之间的区别,您需要检查传入的 Intent 以了解其中包含哪些额外数据。额外的数据将来自您首次启动通知时所做的Intent
。
另外,请确保将 MainActivity
的 LaunchMode
设置为 LaunchMode.SingleTop
,这样您的应用就不会在每次收到通知时重新启动被点击。
[Activity(LaunchMode = LaunchMode.SingleTop, ....)]
public class MainActivity : FormsApplicationActivity {
....
/// <summary>
/// Called when the app is in the background and a notification is clicked on (also called each time the app is minimized and the brought back up), a new <c>Intent</c> is created
/// and sent out, since we use <c>LaunchMode</c> set to <c>SingleTop</c> this method is called instead of the app being restarted.
/// </summary>
/// <param name="intent">The <c>Intent</c> that was set when the call was made. If started from a notification click, extra <c>string</c> values can be extracted.</param>
protected override void OnNewIntent(Intent intent) {
if(intent.HasExtra("Some special key you made up")) { //Here is where you check for special notification intent extras
//Do something brilliant now that you know a notification was clicked on
}
base.OnNewIntent(intent);
}
要了解如何将数据添加到 Intent
,您可以查看 the Xamarin Sport App ,但不要像我总是倾向于做的那样,过多地陷入他们正在做的所有其他事情中。只需关注 PutExtra
部分即可。
编辑#1:
如果您的应用完全关闭,您需要从传递到 OnCreate
的 Intent
中提取数据,并将其传递到您的 App
类中或者用它做其他事情:
protected override async void OnCreate(Android.OS.Bundle bundle) {
base.OnCreate(bundle);
Forms.Init(this, bundle);
string parameterValue = Intent.GetStringExtra("Some special key you made up"); //This would come in from the Push Notification being clicked on
Console.WriteLine("\nIn MainActivity.OnCreate() - Param Intent Extras: {0}\n", parameterValue);
//MessagingCenter.Send("nothing", ConstantKeys.NewNotification); //Do something special with the notification data
LoadApplication(parameterValue != null ? new App(parameterValue) : new App()); //Do something special with the notification data
}
编辑#2:
我会根据我当前的代码建议您对 OnReceive
方法进行一些更改(有些可能不是必需的,但这正是我正在做的事情):
- Label your Broadcast Receiver
- Add stupid Xamarin constructors
- Used constant property instead of string for IntentFilter
- Remove IntentFilter Priority
- Check for null Intent (might not be necessary)
- Use Application.Context instead of Forms.Context (I use Forms.Context in other parts of my app so not sure about this one, but can't hurt)
- Do not overwrite the passed in Intent
- Create startup intent instead of regular
- Add IncludeStoppedPackages flag before pulling out extras
- Check for boot completed event
- Use Notification.Builder instead of NotificationCompat.Builder (though you might need to change this back)
- Add following flags to pendingintent: PendingIntentFlags.UpdateCurrent | PendingIntentFlags.OneShot -- Use NotificationManager (unless you have a specific reason you commented it out)
[assembly: UsesPermission(Android.Manifest.Permission.Vibrate)]
[assembly: UsesPermission(Android.Manifest.Permission.WakeLock)] //Optional, keeps the processor from sleeping when a message is received
[assembly: UsesPermission(Android.Manifest.Permission.ReceiveBootCompleted)] //Allows our app to be opened and to process notifications even when the app is closed
namespace Your.App.Namespace {
[BroadcastReceiver(Enabled = true, Label = "GCM Alarm Notifications Broadcast Receiver")]
[IntentFilter(new []{ Intent.ActionBootCompleted })]
public class AlarmBroadcastReceiver : BroadcastReceiver {
#region Constructors
// ReSharper disable UnusedMember.Global
public AlarmBroadcastReceiver() { }
public AlarmBroadcastReceiver(IntPtr handle, JniHandleOwnership transfer) : base(handle, transfer) { }
// ReSharper restore UnusedMember.Global
#endregion
public void Remind(DateTime dateTime, string msgtype, string usermedid) {
DateTime now = DateTime.Now;
var diffinseconds = (dateTime - now).TotalSeconds;
Intent alarmIntent = new Intent(Application.Context, typeof(AlarmBroadcastReceiver));
alarmIntent.PutExtra("notificationtype", msgtype);
alarmIntent.PutExtra("id", id);
PendingIntent pendingIntent = PendingIntent.GetBroadcast(Application.Context, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
AlarmManager alarmManager = (AlarmManager)Application.Context.GetSystemService(Context.AlarmService);
//TODO: For demo set after 5 seconds.
alarmManager.Set(AlarmType.ElapsedRealtime, SystemClock.ElapsedRealtime() + diffinseconds * 1000, pendingIntent);
}
public override void OnReceive(Context context, Intent intent) {
#region Null Check
if(intent == null) {
Console.WriteLine("\nIn AlarmBroadcastReceiver.OnReceive() - Intent is null\n");
return;
}
#endregion
intent.AddFlags(ActivityFlags.IncludeStoppedPackages);
string action = intent.Action;
Console.WriteLine("\nIn AlarmBroadcastReceiver.OnReceive() - Action: {0}\n", action);
#region Boot Completed Check
if(action.Equals("android.intent.action.BOOT_COMPLETED")) {
PowerManager pm = PowerManager.FromContext(context);
PowerManager.WakeLock sWakeLock = pm.NewWakeLock(WakeLockFlags.Partial, "GCM Broadcast Receiver Tag");
sWakeLock.Acquire();
Console.WriteLine("\nIn AlarmBroadcastReceiver.OnReceive() - Process Shared Preferences Notifications\n");
#region Process Saved Scheduled Notifications
//Get list of saved scheduled notifications that did not fire off before the device was turned off (I store them in SharedPreferences and delete them after they are fired off)
//Go through the list and reschedule them
#endregion
sWakeLock.Release();
return;
}
#endregion
string notificationtype = intent.GetStringExtra("notificationtype");
Intent startupIntent = Application.Context.PackageManager.GetLaunchIntentForPackage(Application.Context.PackageName);
startupIntent.PutExtra("notificationtype", notificationtype);
// Instantiate the builder and set notification elements, including pending intent:
Notification.Builder builder = new Notification.Builder(Application.Context)
.SetDefaults((int)NotificationDefaults.Sound | (int)NotificationDefaults.Vibrate)
.SetAutoCancel(true)
.SetContentIntent(PendingIntent.GetActivity(Application.Context, 0, intent, PendingIntentFlags.UpdateCurrent | PendingIntentFlags.OneShot))
.SetContentTitle("Sample Notification")
.SetContentText("Hello World! This is my first action notification!")
.SetTicker("New Notification")
.SetSmallIcon(Resource.Drawable.icon);
// Build the notification:
Android.App.Notification notification = builder.Build();
// Get the notification manager:
NotificationManager notificationManager = Application.Context.GetSystemService(Context.NotificationService) as NotificationManager;
// Publish the notification:
int notificationId = ??;//This should be a real unique number, otherwise it can cause problems if there are ever multiple scheduled notifications
notificationManager.Notify(notificationId, notification);
}
}
}
关于xamarin - 在 Xamarin Forms Android 中处理通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43936784/
我的设置.py: LANGUAGE_CODE = 'de' TIME_ZONE = require_env("TIME_ZONE_IDENTIFIER") USE_I18N = True USE_L1
谁能给我解释一下 Django 的 forms.Form 和 forms.ModelForm 的相同点和不同点? 最佳答案 从 forms.Form 创建的表单由您手动配置。您最好将这些用于不直接与模
我在用 angularjs 构建的表单上遇到此错误。 `错误:[$compile:multidir] 多个指令 [form, form] 请求 'form' Controller :
我是 Spring 的新手,在尝试显示表单错误时遇到了一些麻烦。 我有以下表格: User Name:
我希望在提交表单时找出 spring:bind 和 form:form 标记库之间的区别。 我的 JSP 片段如下: ....
类型‘AbstractControl’上不存在属性‘Controls’。
有一个问题与此非常相似,但我想以不同的方式提出。 我是一个非常自定的人,但有时我确实喜欢走捷径。就这样吧。 我确实发现这两个类非常相似,尽管其中一个“帮助”程序员更快地编写代码或减少代码/重复代码。将
我在控制台中收到此错误。 “表单提交已取消,因为表单未连接” 自从我们将应用程序迁移到更新版本的 React 后,尝试将我的 redux-form 从 v5 迁移到 v6 之后。 我不确定这里出了什么
我想要的是一个表单,在提交时运行验证检查,并突出显示所有无效字段并添加工具提示。 我正在有效地寻找这样的东西: dojo.forEach(dijit.byId('myForm')._invalidWi
我需要设置symfony2表单元素的值。 我在 Controller 操作中使用了doctrine2实体, Symfony\Component\Form\AbstractType 和createFor
这是用于将数据提交到自定义列表的自定义 Editform.aspx。用户完成表单后,他应该能够点击按钮甚至“确定”按钮,并让 sharepoint 将表单数据提交到列表,然后重定向到项目显示表单 (d
我想知道在 spring 标签中编写所有表单是否是一种好习惯,或者我可以将 spring 表单标签与 html 表单标签混合使用吗? 最佳答案 当您需要 Spring 表单提供的功能时使用它们: 绑定
我正在构建动态表单并希望“即时”添加表单组。 这是我的代码,几乎可以工作: import {Component, OnInit} from '@angular/core'; import {FormG
表格 Form.Load 有什么区别? , Form.Shown和 Form.Activated事件?他们被解雇的顺序是什么? 最佳答案 参见 Windows Forms Events Lifecyc
我正在使用具有路线跟踪功能的 Xamarin Forms 开发一些应用程序。尽管我正在使用 AppCenter,即在 App.xaml.cs OnStart 我添加 protected asy
我正在实现一个 gameboy 模拟器,就像我之前的许多人一样。 我正在尝试实现 PPU 并为此使用代表屏幕的类。 // needed because VS can't find it as depe
我是 Orbeon Form 新手,想使用它。不过,我尝试过 Orbeon Form 网站上的 Form 示例,并用泰语输入了一些数据。是的,可以在“泰语”字段中输入数据。但是当我尝试生成“PDF”时
那么让表单一遍又一遍有效地呈现相同表单的最佳方法是什么,并根据实体的属性值有条件地禁用字段? 我有一个发票实体,需要一个用于创建发票的表单,以及在发票流程的各个阶段(生成、发送、支付等)禁用各个字段的
因此,我一直在与我的同事(开发人员和设计人员)就 Web 表单的自动填充工具进行亲切的辩论。这是一个重要的开发问题,因为它会影响表单的构建方式。 问)自动填充工具(例如 Google 工具栏或 Chr
那么让表单一遍又一遍有效地呈现相同表单的最佳方法是什么,并根据实体的属性值有条件地禁用字段? 我有一个发票实体,需要一个用于创建发票的表单,以及在发票流程的各个阶段(生成、发送、支付等)禁用各个字段的
我是一名优秀的程序员,十分优秀!