First of all, I am developing an app in C# Xamarin.form. But any advice not related to Xamarin is welcome as well.
As described in the title, I am able to receive notifications no matter when the app is in background, foreground or killed.
But the problem is that I can't open (re-activate would be the better word) the app (let's say I hit android home button) when I click the foreground-received notification. Otherwise, things are all working as expected.
The server sends FCM with notification and data;
{
data: {
topic : "push",
title : subject,
body : contents,
...data,
},
priority : "high",
notification: {
title: subject,
body: contents,
},
tokens,
};
{
// [FirebaseMessagingService]
var notificationBuilder = new NotificationCompat.Builder(this, MainActivity.CHANNEL_ID)
.SetContentTitle(title)
.SetContentText(messageBody)
.SetPriority((int)NotificationPriority.Max)
.SetAutoCancel(true)
.SetContentIntent(StartActivity(url))
.SetSmallIcon(Resource.Mipmap.icon)
.SetStyle(new NotificationCompat.BigTextStyle().BigText(messageBody));
var timeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
var timestamp = timeSpan.TotalSeconds;
NotificationManager notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
notificationManager.Notify((int)timestamp, notificationBuilder.Build());
}
public PendingIntent StartActivity(string _url)
{
Intent intent = new Intent(MainActivity.instance, typeof(FCMReceiver));
//Intent intent = new Intent(MainActivity.instance, typeof(MainActivity));
//MainActivity.instance.Intent.AddFlags(ActivityFlags.ClearTop);
MainActivity.instance.Intent.AddFlags(ActivityFlags.NewTask);
MainActivity.instance.Intent.SetAction("notification_cancelled");
MainActivity.instance.Intent.PutExtra("url", _url);
intent.AddFlags(ActivityFlags.NewTask);
intent.SetAction("notification_cancelled");
intent.PutExtra("url", _url);
//DataManager.RedirectUrl = _url;
var timeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
var timestamp = timeSpan.TotalSeconds;
return PendingIntent.GetBroadcast(MainActivity.instance, (int) timestamp, intent, PendingIntentFlags.OneShot);
//return PendingIntent.GetActivity(MainActivity.instance, 5000, intent, PendingIntentFlags.CancelCurrent);
}
[BroadcastReceiver(Enabled = true, Exported = false)]
public class FCMReceiver : BroadcastReceiver
{
public override void OnReceive(Context _context, Intent intent)
{
Device.BeginInvokeOnMainThread(() =>
{
// .. does nothing
});
}
}
Related
I am using FCM along with PHP to send notification. It is working fine when,
App is in foreground, background and killed (below oreo )
App is in
foreground, background and not when killed(in oreo)
I even tried creating Notification Channel and tried removing 'notification' from php script too but nothing works.I have tried all the duplicate answers in SO but nothing works. Any help would be great. Thanks.
Here is my code,
#SuppressLint("NewApi")
private void sendNotification1(RemoteMessage remoteMessage) {
Log.e("remoteMessage", remoteMessage.getData().toString());
String title = remoteMessage.getNotification().getTitle();
String body = remoteMessage.getNotification().getBody();
Intent resultIntent = new Intent(getApplicationContext(), SplashActivity.class);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),
0 /* Request code */, resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultsound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
OreoNotification oreoNotification = new OreoNotification(this);
Notification.Builder builder = oreoNotification.getOreoNotification(title, body, pendingIntent, defaultsound, String.valueOf(R.drawable.appicon3copy));
int i = 0;
oreoNotification.getManager().notify(i, builder.build());
}
Here is the OreoNotification Class,
public class OreoNotification extends ContextWrapper {
private static final String CHANNEL_ID = "Fcm Test";
private static final String CHANNEL_NAME = "Fcm Test";
private NotificationManager notificationManager;
public OreoNotification(Context base) {
super(base);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannel();
}
}
#TargetApi(Build.VERSION_CODES.O)
private void createChannel() {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("Fcm Test channel for app test FCM");
channel.enableLights(true);
channel.enableVibration(true);
channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
channel.setShowBadge(false);
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
getManager().createNotificationChannel(channel);
}
public NotificationManager getManager() {
if (notificationManager == null) {
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return notificationManager;
}
#TargetApi(Build.VERSION_CODES.O)
public Notification.Builder getOreoNotification(String title, String body, PendingIntent pendingIntent, Uri soundUri, String icon) {
return new Notification.Builder(getApplicationContext(), CHANNEL_ID)
.setAutoCancel(true)
.setSmallIcon(R.drawable.appicon3copy)
.setContentTitle(title)
.setContentText(body)
.setContentIntent(pendingIntent);
}
}
Here is the part of my PHP Script,
$msg = array
(
'body' => utf8_encode('Firebase Push Notification'),
'title' => utf8_encode('Anusha Kumar'),
'click_action' => ('.SplashActivity'),
);
$fields = array
(
'to' => $_REQUEST['token'],
'notification' => $msg , // tried removing this line too but doesn't work
'data'=>$msg,
);
I am using azure hub notification.
I am developing my app in Xamarin.Forms. For android I can get notification when I test it hits to debug and i can show a DisplayAlert for that.
But I cannot show it as notification. I searched and after android oreo I should create a notification channel.
But I don't know how to do it. They are saying that you should create a notification id in your strings.xml but I don't have strings.xml file. I dont know how to do it, can anyone help?
internal static readonly string CHANNEL_ID = "cross_channel";
void CreateNotification(string title, string desc)
{
var notificationManager = GetSystemService(Context.NotificationService)
as NotificationManager;
var uiIntent = new Intent(this, typeof(MainActivity));
var pendingIntent = PendingIntent.GetActivity(this, RandomGenerator(), uiIntent, PendingIntentFlags.OneShot);
var notification = new Notification(Android.Resource.Drawable.ButtonMinus, title)
{
Flags = NotificationFlags.AutoCancel
};
notification.SetLatestEventInfo(this, title, desc,
PendingIntent.GetActivity(this, 0, uiIntent, 0));
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
var channel = new NotificationChannel(CHANNEL_ID,
"Cross Notifications",
NotificationImportance.High);
notificationManager.CreateNotificationChannel(channel);
string channelId = "Cross Channel";
var notBuilder = new Notification.Builder(Application.Context, CHANNEL_ID).SetContentTitle(title).SetContentText(desc).SetSmallIcon(Android.Resource.Drawable.StarBigOn).SetAutoCancel(true);
notificationManager.Notify(1, notBuilder.Build());
channel.Description = (desc);
notBuilder.SetChannelId(channelId);
}
notificationManager.Notify(RandomGenerator(), notBuilder.Build());
}
I had a bit of trouble getting notifications properly showing on with Xamarin.Forms also. I'm assuming you have overridden the "OnMessageReceived" event and you're calling "CreateNotification" directly? In the end, this was the code that worked for me:
private void ShowNotification(RemoteMessage msg, IDictionary<string, string> data)
{
var intent = new Intent();
intent.AddFlags(ActivityFlags.ClearTop);
foreach (var key in data.Keys)
intent.PutExtra(key, data[key]);
var pendingIntent = PendingIntent.GetActivity(Android.App.Application.Context, 100, intent, PendingIntentFlags.OneShot);
var notificationBuilder = new NotificationCompat.Builder(Android.App.Application.Context) // Note: Everything I read online said to provide the ChannelID string here, but doing so caused it to not display notifications.
.SetSmallIcon(Resource.Drawable.abc_btn_radio_to_on_mtrl_000) // You can set this to your apps icon
.SetContentTitle(msg.GetNotification().Title)
.SetContentText(msg.GetNotification().Body)
.SetPriority((int)Android.App.NotificationImportance.Max)
.SetDefaults(NotificationCompat.DefaultAll)
.SetContentIntent(pendingIntent) // Even though intent here is empty, you *may* need to include it for the notification to show, I never tried without one.
.SetVisibility((int)NotificationVisibility.Public);
var notificationManager = NotificationManagerCompat.From(Android.App.Application.Context);
notificationManager.Notify(100, notificationBuilder.Build());
}
In the MainActivity.cs You can call this method in the onCreate method :
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 channel = new NotificationChannel(CHANNEL_ID, "FCM Notifications", NotificationImportance.Default)
{
Description = "Firebase Cloud Messages appear in this channel"
};
var notificationManager = (NotificationManager) GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
}
Where
internal static readonly string CHANNEL_ID = "my_notification_channel";
internal static readonly int NOTIFICATION_ID = 100;
are the definition for channel id and notification id respectively.
In the MainActivity's OnCreate after loading XF call this :
LoadApplication(new App());
CreateNotificationChannel();
Good luck
Revert in case of queries
How to add the fcm notification in inbox style when app is in background?
When i add the the below code i got inbox style when app is open
but if the app is background it showing seperate notification
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
Integer notify_no = 0;
Integer numMessages = 0;
DBHelper db = new DBHelper(this);
private final int notificationID = 237;
private static int value = 0;
// Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
//Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.newlogo);
// TODO(developer): Handle FCM messages here.
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
/* Integer badge = Integer.parseInt(remoteMessage.getData().get("badge"));
Log.d("notificationNUmber",":"+badge);
setBadge(getApplicationContext(), badge);*/
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
Intent intent = new Intent();
intent.setAction("com.ksoft.propreka.CUSTOM_INTENT");
sendBroadcast(intent);
db.insertNotification(remoteMessage.getNotification().getBody(),remoteMessage.getData().get("room_id"));
//EventBus.getDefault().post(remoteMessage.getNotification().getBody());
//
try {
if (remoteMessage.getNotification() != null) {
sendNotification(remoteMessage.getData().get("text"));
} else if (!remoteMessage.getData().isEmpty()) {
sendNotification(remoteMessage.getData().get("text"));
}
} catch (Exception e) {
Log.d("json error", e.toString());
}
//sendNotification(remoteMessage.getData().get("text"));
Log.d("test",":test notification");
//createpushnotification();
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}
/**
* Create and show a simple notification containing the received FCM message.
*
* #param messageBody FCM message body received.
*/
public void sendNotification(String messageBody) {
Intent intent = new Intent(this,Main2Activity.class);
intent.putExtra("messages","messages");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra("fcm_notification", "Y");
PendingIntent pendingIntent = PendingIntent.getActivity(this,0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle("Propreka")
.setSmallIcon(R.mipmap.new_logo)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(Uri.parse("content://settings/system/notification_sound"))
.setVibrate(new long []{100,2000,500,2000})
.setContentIntent(pendingIntent);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(getResources().getString(R.string.app_name));
Integer msg_count = db.message_count();
Integer chat_count = db.chat_count();
inboxStyle.setSummaryText(" "+msg_count+" messages from "+chat_count+" chat");
ArrayList<ArrayList> Newchat = db.getNotifications();
for (ArrayList s : Newchat) {
inboxStyle.addLine(s.get(0).toString());
}
notificationBuilder.setStyle(inboxStyle);
NotificationManager notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
/*Intent resultIntent = new Intent(getBaseContext(), Main2Activity.class);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent piResult = PendingIntent.getActivity(this, 1, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.new_logo)
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(messageBody)
.setVibrate(new long []{0,100,10,100})
.setContentIntent(piResult);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
// String[] events = new String[6];
inboxStyle.setBigContentTitle(getResources().getString(R.string.app_name));
ArrayList<ArrayList> Newchat = db.getNotifications();
for (ArrayList s : Newchat) {
inboxStyle.addLine(s.get(0).toString());
}
mBuilder.setStyle(inboxStyle);
nManager.notify(getResources().getString(R.string.app_name),0 ,mBuilder.build());*/
}
public void createpushnotification()
{
Log.i("Start", "notification");
/* Invoking the default notification service */
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle("New Message");
mBuilder.setContentText("You've received new message.");
mBuilder.setTicker("New Message Alert!");
mBuilder.setSmallIcon(R.mipmap.new_logo);
/* Increase notification number every time a new notification arrives */
mBuilder.setNumber(++numMessages);
/* Add Big View Specific Configuration */
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
String[] events = new String[6];
events[0] = new String("This is first line....");
events[1] = new String("This is second line...");
events[2] = new String("This is third line...");
events[3] = new String("This is 4th line...");
events[4] = new String("This is 5th line...");
events[5] = new String("This is 6th line...");
// Sets a title for the Inbox style big view
inboxStyle.setBigContentTitle("Big Title Details:");
// Moves events into the big view
for (int i=0; i < events.length; i++) {
inboxStyle.addLine(events[i]);
}
mBuilder.setStyle(inboxStyle);
/* Creates an explicit intent for an Activity in your app */
Intent resultIntent = new Intent(this, Main2Activity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(Main2Activity.class);
/* Adds the Intent that starts the Activity to the top of the stack */
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
/* notificationID allows you to update the notification later on. */
mNotificationManager.notify(notificationID, mBuilder.build());
}
}
how to add the inbox style?
Use data payload to send the notification data and show it the phone using this class.
For example:
With Notification payload
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
}
}
This data will show default notification to the phone by the firebase when the app is in background but the onMessageReceived method from FirebaseMessagingService will be called when app is in foreground.
With data payload
Everytime you send notification onMessageReceived method will be called. So you can build notification as you want.
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
},
}
For more detail head out to the the official documentation here.
There is two type of FCM messages.
Notification Message.
Data Messages.
FCM
Send data message, then it will come in your call method.
When application in background then, FCM not call the onMessageReceived method.
Instead of it display the default notification.
When the iconIndex of my notification = 0, it looks for the name in the array on position 0, and returns "ic_alert", which is the name of the icon. But only when this icon is chosen, it doesn't want to play the notification sound, even though isPlaySound() returns true, so weird... Other icons work fine
public class AlarmReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Log.e("ALARMRECEIVER","ONRECEIVE");
//Create a notification
long notificationId = intent.getLongExtra("id", -1);
if(notificationId == -1)
{
Log.e("AlarmReceiver","id went missing");
}
else
{
NotificationRepository repository = NotificationRepository.getInstance(context);
Notification notification = repository.getNotification(notificationId);
if(notification != null)
{
String[] icons = context.getResources().getStringArray(R.array.icons);
int iconId = context.getResources().getIdentifier(context.getPackageName()
+ ":drawable/" + icons[notification.getIconIndex()], null, null);
String icon = icons[notification.getIconIndex()];
//create the android notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(iconId)
.setContentTitle(notification.getTitle())
.setContentText(notification.getSubtitle())
.setColor(ContextCompat.getColor(context, R.color.colorPrimary));
if(notification.isPlaySound())
{
mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
Log.e("ALARMRECEIVER", "SOUND");
}
else Log.e("ALARMRECEIVER","NO SOUND");
if (notification.isVibrate())
{
mBuilder.setVibrate(new long[]{1000, 1000});
}
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify((int) notificationId, mBuilder.build());
//Delete the notification from the database
repository.removeNotification(notificationId);
Intent i = new Intent("dvanack.gmail.com.NOTIFY");
context.sendBroadcast(i);
Log.w("ONRECEIVE","ENDED");
}
I don't have this problem with other devices than the One Plus one running Cyanogenmod, this was fixed the day after with an update ...
I'm using Google Play Services lib for GCM and all works great.
Now I implement 2 buttons (actions) in my notificationCompat.Builder:
- One open app.
- Other one delete some unread messages: for this operation I post some dato to server.
How can I launch a post to my server on notification action click without open any activity in front of user?
Thank you very much!!!!!
Here my code:`Intent openIntent = new Intent();
Intent cancelIntent = new Intent();
if (has1Id) {
//go details!
openIntent = new Intent(this, NotificationActivity.class);
openIntent.putExtra("id", ids.get(0));
//cancell intent: start async operation in background
cancelIntent = new Intent(this, HomeActivity.class);
cancelIntent.putExtra("id", ids.get(0));
}else{
//go home!
openIntent = new Intent(this, HomeActivity.class);
//cancell intent: start async operation in background
cancelIntent = new Intent(this, HomeActivity.class);
cancelIntent.putExtra("id", ids);
}
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
openIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent cancelNotificationIntent = PendingIntent.getActivity(this, 0,
cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT);
long[] vibrationPattern = {0, 200, 800, 200, 100, 500};
Uri soundUri = Uri.parse("android.resource://"+ getPackageName() + "/" + R.raw.jingle1);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.ic_notification)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setContentTitle(title) //set title
.setContentInfo(numNotRead) //number of unread content
.setAutoCancel(true)
.setContentText(msgs.get(0)+"...")
.setVibrate(vibrationPattern)
.setSound(soundUri)
.setLights(Color.rgb(255, 165, 00), 800, 800)
.setContentIntent(contentIntent).addAction(R.drawable.ic_notification, getString(R.string.open_app), contentIntent)
.setContentIntent(cancelNotificationIntent).addAction(android.R.drawable.ic_delete, getString(R.string.mark_read), cancelNotificationIntent);
if (has1Id) {
NotificationCompat.BigTextStyle bigTextStyle =new NotificationCompat.BigTextStyle().bigText(msgs.get(0));
mBuilder.setContentTitle(getString(R.string.notification_new_content));
//get only 1 long text: the description of content
mBuilder.setStyle(bigTextStyle);
}else{
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(getString(R.string.notification_new_contents));
for (int i=0; i < msgs.size(); i++) {
inboxStyle.addLine(msgs.get(i));
}
mBuilder.setStyle(inboxStyle);
}
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());`
You should use another class as the intent receiver for cancelIntent, like a BroadcastReceiver and handle the intent there with no UI.
public class MyIntentReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context _context, Intent _intent) {
if (_intent.getAction().equals(MY_INTENT)) {
// TODO Handle a notification
}
}
}
You also can use a Activity with no UI as the intent, but I think previous option is better.
More info:
Android Activity with no GUI