我的应用创建了安排在不同时间安排的多个本地通知。这些通知都有不同的通知ID,当用户点击通知时,我的应用应该显示来自数据库的唯一记录。但是,我发现所有的水龙头都向我返回了相同的通知ID。该ID始终是我安排的最后一个通知的ID。有人知道为什么吗?
我创建了一个简化的sln来说明此问题。当用户单击单个按钮时,它将在4秒钟内创建4个唯一通知,但是当用户在4个通知中的每个选项卡上使用时,将重复显示相同的通知ID。下面是调试输出:
[0:] notificationId: 49617
[0:] notificationId: 49621
[0:] notificationId: 49622
[0:] notificationId: 49623
[0:] bundleContent: 49623
[0:] bundleContent: 49623
[0:] bundleContent: 49623
[0:] bundleContent: 49623
我的MainActivity:
////////////////////////////////// MainActivity.cs
using System;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Support.V4.App;
using Android.Support.V7.App;
using Android.Widget;
using Java.Lang;
using TaskStackBuilder = Android.Support.V4.App.TaskStackBuilder;
namespace Notifications
{
[Activity(Label = "Notifications", MainLauncher = true, Icon = "@drawable/Icon")]
public class MainActivity : AppCompatActivity
{
// Unique ID for our notification:
static readonly string CHANNEL_ID = "location_notification";
internal static readonly string BUNDLE_DATA_KEY = "BUNDLE_DATA_KEY";
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
CreateNotificationChannel();
// Display the "Hello World, Click Me!" button and register its event handler:
var button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += ButtonOnClick;
}
// Handler for button click events.
void ButtonOnClick(object sender, EventArgs eventArgs)
{
CreateNotification();
CreateNotification();
CreateNotification();
CreateNotification();
}
// Create notification with a unique ID after sleeping for one second
void CreateNotification()
{
// Sleep for a second
System.Threading.Thread.Sleep(1000);
double running;
int notificationId;
Intent resultIntent;
Bundle valuesForActivity;
TaskStackBuilder stackBuilder;
NotificationManagerCompat notificationManager = NotificationManagerCompat.From(this);
NotificationCompat.Builder builder;
PendingIntent resultPendingIntent;
// Generate a unique notificationId
running = DateTime.Now.Subtract(new DateTime(2018, 12, 30, 0, 0, 0)).TotalSeconds;
notificationId = (int)running;
System.Diagnostics.Debug.Write(notificationId.ToString(), "notificationId");
// Create the value bundle
valuesForActivity = new Bundle();
valuesForActivity.PutInt(BUNDLE_DATA_KEY, notificationId);
// When the user clicks the notification, SecondActivity will start up.
resultIntent = new Intent(this, typeof(SecondActivity));
// Pass some values to SecondActivity:
resultIntent.PutExtras(valuesForActivity);
// Construct a back stack for cross-task navigation:
stackBuilder = TaskStackBuilder.Create(this);
stackBuilder.AddParentStack(Class.FromType(typeof(SecondActivity)));
stackBuilder.AddNextIntent(resultIntent);
// Create the PendingIntent with the back stack:
resultPendingIntent = stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);
// Build the notification:
builder = new NotificationCompat.Builder(this, 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("Button Clicked") // Set the title
.SetNumber(1) // Display the count in the Content Info
.SetSmallIcon(Resource.Drawable.ic_stat_button_click) // This is the icon to display
.SetContentText($"The number is {notificationId}."); // the message to display.
// Finally, publish the notification:
notificationManager.Notify(notificationId, builder.Build());
}
void CreateNotificationChannel()
{
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
{
// Notification channels are new in API 26 (and not a part of the
// support library). There is no need to create a notification
// channel on older versions of Android.
return;
}
var name = Resources.GetString(Resource.String.channel_name);
var description = GetString(Resource.String.channel_description);
var channel = new NotificationChannel(CHANNEL_ID, name, NotificationImportance.Default)
{
Description = description
};
var notificationManager = (NotificationManager) GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
}
}
}
我的SecondActivity:
////////////////////////////////// SecondActivity.cs
using System;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Support.V7.App;
using Android.Widget;
namespace Notifications
{
[Activity(Label = "Second Activity")]
public class SecondActivity : AppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Get the count value passed to us from MainActivity:
int contentInsideBundle = Intent.Extras.GetInt(MainActivity.BUNDLE_DATA_KEY, -1);
// Display the count sent from the first activity:
SetContentView(Resource.Layout.Second);
var txtView = FindViewById<TextView>(Resource.Id.textView1);
txtView.Text = $"The number was {contentInsideBundle}.";
// Write Debug
System.Diagnostics.Debug.Write(contentInsideBundle.ToString(), "bundleContent");
}
}
}
答案 0 :(得分:1)
resultPendingIntent = stackBuilder.GetPendingIntent(0,(int)PendingIntentFlags.UpdateCurrent);
Extra将被更新为最后一个传入Intent的Extra,因此您将获得相同的NotificationId。如果需要为每个通知获取正确的ID,则在定义意图时,还需要区分意图!您可以在意图下添加代码,如下所示:
public function showajax($id){
$modules= modules::where('project_id',$id)->get();
foreach($modules as $row)
{
$html[]=
'<td>' . $row->id . '</td>' .
'<td>' . $row->title . '</td>' .
'<td>' . $row->status. '</td>'.
'<br/>';
}
return Response::json($html);
}