私のアプリでボタンをクリックすると、通知(通知とアラーム)を表示します。私は通知を作成するためにBroadcastReceiverを作成しました。これは、フラグメント内のボタンをクリックすると呼び出されます。ここでブロードキャストレシーバのAndroidで通知を表示できません
は、私がこれまで行っているものです: -
public class AlarmReceiver : BroadcastReceiver
{
private int resourceId;
public override void OnReceive(Context context, Intent intent)
{
var message = intent.GetStringExtra("message");
var title = intent.GetStringExtra("title");
var mEvent = intent.GetStringExtra("event");
var notIntent = new Intent(context, typeof(SplashActivity));
var contentIntent = PendingIntent.GetActivity(context, 0, notIntent, PendingIntentFlags.CancelCurrent);
var manager = NotificationManagerCompat.From(context);
var style = new NotificationCompat.BigTextStyle();
style.BigText(message);
if (mEvent == "Birthday")
resourceId = Resource.Drawable.ic_cupcake;
else if (mEvent== "Anniversary")
resourceId = Resource.Drawable.ic_anniversary;
var wearableExtender = new NotificationCompat.WearableExtender().SetBackground(BitmapFactory.DecodeResource(context.Resources, resourceId));
//Generate a notification with just short text and small icon
var builder = new NotificationCompat.Builder(context)
.SetContentIntent(contentIntent)
.SetSmallIcon(Resource.Drawable.ic_applogo)
.SetContentTitle(title)
.SetContentText(message)
.SetDefaults(1)
.SetStyle(style)
.SetWhen(Java.Lang.JavaSystem.CurrentTimeMillis())
.SetAutoCancel(true);
var notification = builder.Build();
manager.Notify(0, notification);
}
}
ReminderFragment
public static void Remind(Activity mActivity, DateTime dateTime, string title, string message, string eventType)
{
Intent alarmIntent = new Intent(mActivity, typeof(AlarmReceiver));
alarmIntent.PutExtra("message", message);
alarmIntent.PutExtra("title", title);
alarmIntent.PutExtra("event",eventType);
PendingIntent pendingIntent = PendingIntent.GetBroadcast(mActivity, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
AlarmManager alarmManager = (AlarmManager)mActivity.GetSystemService(Context.AlarmService);
//TODO: For demo set after 5 seconds.
alarmManager.Set(AlarmType.ElapsedRealtime, SystemClock.ElapsedRealtime() + 5 * 1000, pendingIntent);
}
btnRemind.Click += delegate
{
var msg = "Wish " + Name + "on their " + eventType;
Remind(mActivity, DateTime.Now, "Reminder", msg, eventType);
Toast.MakeText(mActivity, "Reminder set", ToastLength.Long).Show();
};
マニフェスト
<application android:label="Events" android:icon="@drawable/Icon">
<receiver android:name=".AlarmReceiver" android:enabled="true">
</receiver>
</application>
SplashActivity
私のランチャーの活動です。しかし、フラグメントはMainActivity
という別のアクティビティから表示されます(それは問題である可能性があります) テストの目的で、私は5秒後に通知を表示するようにアプリを有効にしましたが、何とか動作していないため通知が表示されません。
すべてのヘルプは、メインの活動開始にレシーバを登録し、終了時に登録を解除してください
メインアクティビティの開始時にブロードキャストレシーバを登録していますか? – Ramit
@Ramitそれを指摘してくれてありがとう。それは愚かな間違いでした。私は受信機を登録することを忘れました。私はそれを受け入れることができるようにあなたの答えを投稿できますか? –
私は答えを加えました。これに加えて、(マニフェスト自体から)あなたの問題を解決する他の方法があることを言及したいと思います。私はこれらを掘り下げており、あなたが時間を得れば、一度行った答えを更新します。また探検してください。 – Ramit