[英]Xamarin.Android: Clicking on Notification Causes Whole App to Blank
My Xamarin.Android notification is causing the whole app to go blank when it's clicked.我的 Xamarin.Android 通知在单击时导致整个应用程序变为 go 空白。 I had the typeof
set to a Fragment
, which caused the notification to not start my app at all.我将typeof
设置为Fragment
,这导致通知根本无法启动我的应用程序。
Here is the code I'm using to generate my notifications:这是我用来生成通知的代码:
public static int _count = 0;
public async void SendNotifications()
{
await Task.Run(() =>
{
var _ctx = Android.App.Application.Context;
//I've also tried setting the bundle to MainActivity existing bundle with no change
//Bundle _bundle = MainActivity._bundle;
var valuesForActivity = new Bundle();
valuesForActivity.PutInt(MainActivity.COUNT_KEY, _count);
//I'd eventually like to pass a url string foreach notification but have
// disabled this line for the sake of simplicity.
//valuesForActivity.PutString(_main._notificationString, "https://bitchute.com/subscriptions/");
// When the user clicks the notification, MainActivity will start up.
// This line works, but it causes the app to go blank
var resultIntent = new Intent(_ctx, typeof(MainActivity));
// Pass some values to MainActivity:
resultIntent.PutExtras(valuesForActivity);
// Construct a back stack for cross-task navigation:
var stackBuilder = Android.Support.V4.App.TaskStackBuilder.Create(_ctx);
//stackBuilder.AddParentStack(Class.FromType(typeof(MainActivity)));
stackBuilder.AddNextIntent(resultIntent);
_fm5.GetPendingIntent();
// Create the PendingIntent with the back stack:
var resultPendingIntent = stackBuilder.GetPendingIntent(0, (int)Android.App.PendingIntentFlags.UpdateCurrent);
foreach (var note in ExtNotifications._customNoteList)
{
// Build the notification:
var builder = new Android.Support.V4.App.NotificationCompat.Builder(_ctx, MainActivity.CHANNEL_ID)
.SetAutoCancel(true) // Dismiss the notification from the notification area when the user clicks on it
.SetContentIntent(resultPendingIntent) // Start up this activity when the user clicks the intent.
.SetContentTitle(note._noteType) // Set the title
.SetNumber(_count) // Display the count in the Content Info
.SetSmallIcon(Resource.Drawable.bitchute_notification2) // This is the icon to display
.SetContentText(note._noteText);
// Finally, publish the notification:
var notificationManager = Android.Support.V4.App.NotificationManagerCompat.From(_ctx);
notificationManager.Notify(MainActivity.NOTIFICATION_ID, builder.Build());
_count++;
}
});
}
The notification text generates fine from this code:通知文本从此代码生成良好:
https://github.com/hexag0d/BitChute_Mobile_Android_BottomNav/blob/CookieShare/Classes/ExtNotifications.cs https://github.com/hexag0d/BitChute_Mobile_Android_BottomNav/blob/CookieShare/Classes/ExtNotifications.cs
What I'd really like to do is load a url in one of my fragments, by passing it into the notification, but the first step is having my app not go blank on notification clicked;我真正想做的是在我的一个片段中加载 url,通过将其传递到通知中,但第一步是让我的应用程序不是 go 在单击通知时为空白; can anyone tell me what I did wrong here?谁能告诉我我在这里做错了什么?
Also, I'm not using firebase;另外,我没有使用 firebase; I'm just using the vanilla Android notification API, and would prefer to keep it as simple as possible.我只是使用香草 Android 通知 API,并且希望尽可能简单。
EDIT: I forgot to mention that I've also tried setting android:launchMode="singleInstance"
in the app manifest as-well as singleTop
and others.编辑:我忘了提到我还尝试在应用程序清单中设置android:launchMode="singleInstance"
以及singleTop
和其他。 That didn't work.那没有用。
You actually need to init an Activity in your pending intent without creating a different Task thread so your SendNotification method would look like:实际上,您需要在未决意图中初始化一个 Activity 而无需创建不同的任务线程,因此您的 SendNotification 方法如下所示:
void SendNotification(string messageBody)
{
try
{
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.SetSmallIcon(Resource.Drawable.ic_stat_ic_notification)
.SetContentTitle("Title")
.SetContentText(messageBody)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
NotificationManagerCompat notificationManager = NotificationManagerCompat.From(this);
notificationManager.Notify(0, notificationBuilder.Build());
}
catch (Exception ex)
{
}
}
Once you do that you can just call your Fragment from this Activity by using some flag like a bundled boolean完成此操作后,您可以使用捆绑的 boolean 之类的标志从该活动中调用您的片段
I fixed this issue by handling OnNewIntent
inside MainActivity
, also changing some of the flags, and way that I built the notification.我通过处理MainActivity
中的OnNewIntent
解决了这个问题,还更改了一些标志,以及我构建通知的方式。 I unfortunately changed a bunch code for how the notification was built at once, at the same time that I handled OnNewIntent
, so I'm not sure if all of these changes are necessary.不幸的是,我在处理OnNewIntent
的同时更改了如何立即构建通知的一堆代码,所以我不确定是否所有这些更改都是必要的。 I do know that handling OnNewIntent
was absolutely essential;我知道处理OnNewIntent
是绝对必要的。 what I don't know is whether or not changing the parameters for the notification builder was necessary.我不知道是否有必要更改通知生成器的参数。 Here is the full working code:这是完整的工作代码:
//the notification class
public class CustomNotification
{
public string _noteType { get; set; }
public string _noteText { get; set; }
public string _noteLink { get; set; }
public int _noteIndex { get; set; }
}
//this has to be in MainActivity
protected override void OnNewIntent(Intent intent)
{
string url = "";
base.OnNewIntent(intent);
if (intent != null)
{
url = intent.Extras.GetString("URL");
}
try
{
switch (_viewPager.CurrentItem)
{
case 0:
_fm1.LoadCustomUrl(url);
break;
case 1:
_fm2.LoadCustomUrl(url);
break;
case 2:
_fm3.LoadCustomUrl(url);
break;
case 3:
_fm4.LoadCustomUrl(url);
break;
case 4:
_fm5.LoadCustomUrl(url);
break;
}
}
catch
{
}
}
public void SendNotifications(List<CustomNotification> notificationList)
{
try
{
var _ctx = Android.App.Application.Context;
int _noteCount = 0;
foreach (var note in notificationList)
{
var resultIntent = new Intent(_ctx, typeof(MainActivity));
var valuesForActivity = new Bundle();
valuesForActivity.PutInt(MainActivity.COUNT_KEY, _count);
MainActivity._NotificationURLList.Add(note._noteLink);
valuesForActivity.PutInt("Count", _noteCount);
valuesForActivity.PutString("URL", note._noteLink);
resultIntent.PutExtras(valuesForActivity);
//var resultPendingIntent = PendingIntent.GetActivity(_ctx, 0, resultIntent, PendingIntentFlags.UpdateCurrent);
//set to unique ID per Leo Zhu - MSFT
var resultPendingIntent = PendingIntent.GetActivity(_ctx, MainActivity.NOTIFICATION_ID, resultIntent, PendingIntentFlags.UpdateCurrent);
resultIntent.AddFlags(ActivityFlags.SingleTop);
var builder = new Android.Support.V4.App.NotificationCompat.Builder(_ctx, MainActivity.CHANNEL_ID)
.SetAutoCancel(true) // Dismiss the notification from the notification area when the user clicks on it
.SetContentIntent(resultPendingIntent) // Start up this activity when the user clicks the intent.
.SetContentTitle(note._noteType) // Set the title
.SetNumber(_count) // Display the count in the Content Info
//xamarin was all whacked out so I had to set this static int from ctrl + F (resource) in resourcedesigner.cs
.SetSmallIcon(2130837590) // This is the icon to display
.SetContentText(note._noteText);
MainActivity.NOTIFICATION_ID++;
// Finally, publish the notification:
var notificationManager = Android.Support.V4.App.NotificationManagerCompat.From(_ctx);
notificationManager.Notify(MainActivity.NOTIFICATION_ID, builder.Build());
_count++;
_noteCount++;
//I think if the notification count gets too high android kills the app? I set this very high just in case
if (_count >= 300)
{
_count = 0;
return;
}
}
}
catch
{
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.