LoginSignup
2
1

More than 5 years have passed since last update.

Firebase プッシュ通知 で複数行の表示 (Android, Xamarin, Azure)

Posted at

Firebaseのプッシュ通知(FCM)で通知領域に複数行表示させるメモ。
通知はAzureから送信している。
Xamarinで開発しているので言語は、C#(関数が大文字で始まってるので、Javaなら小文字に変換が必要)
Android8以上では、NotificationChannelが必要。

SendNotification(title, body);

void SendNotification(string title, string body)
{
    const string CHANNEL_ID = "MY_CHANNEL_ID";
    CreateNotificationChannel(CHANNEL_ID);

    Intent intent = new Intent(this, typeof(MainActivity));
    const int pendingId = 0;
    PendingIntent pendingIntent = PendingIntent.GetActivity(this, pendingId, intent, PendingIntentFlags.OneShot);

    if (Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.O)
        title = new Models.Settings().NotifTitle;

// 複数行表示するには、BigTextStyleを作成する
    NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle()
        .SetBigContentTitle(title)
        .BigText(body);

// 作成したBigTextStyleをSetStyle()でセットする
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
        .SetContentIntent(pendingIntent)
        .SetSmallIcon(Resource.Drawable.ic_stat_ic_notification)
        //.SetContentTitle(title)
        //.SetContentText(body)
        .SetStyle(bigTextStyle)
        .SetDefaults(-1)//NotificationDefaults.Sound | NotificationDefaults.Vibrate | NotificationDefaults.Lights);
        .SetAutoCancel(true);


    NotificationManagerCompat manager = NotificationManagerCompat.From(this);
    const int notifId = 0;
    manager.Notify(notifId, builder.Build());
    //StartForeground(id, builder.Build());
}

void CreateNotificationChannel(string channelId)
{
    if (Android.OS.Build.VERSION.SdkInt < Android.OS.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 set = new Models.Settings();
    var channel = new NotificationChannel(channelId, set.ChannelName, NotificationImportance.Default)
    {
        Description = set.ChannelDescription
    };
    // 通知チャンネルの設定のデフォルト値。設定必須ではなく、ユーザーが変更可能。
    //channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
    channel.EnableVibration(true);
    channel.EnableLights(true);
    //channel.SetLightColor(argb);
    //channel.SetSound(uri, audioAttributes);
    channel.SetShowBadge(false);

    var notificationManager = (NotificationManager)GetSystemService(NotificationService);
    notificationManager.CreateNotificationChannel(channel);
}

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1