How to use BroadcastReceiver in Xamarin with C #? - c#

I need to generate a notification when the app is in the background, but I'm not sure how to start Broadcastreceiver

There are two ways:
1. LocalBroadcastManager will send messages only to your application
Intent message = new Intent("com.xamarin.example.TEST");
// If desired, pass some values to the broadcast receiver.
intent.PutExtra("key", "value");
Android.Support.V4.Content.LocalBroadcastManager.GetInstance(this).SendBroadcast(message);
2. Context.SendBroadcast methods will send messages to the entire system
Intent message = new Intent("com.xamarin.example.TEST");
// If desired, pass some values to the broadcast receiver.
intent.PutExtra("key", "value");
context.SendBroadcast(intent);
from Xamarin guide

Related

How to use FCMClient to send messages to both Android and iPhone

Im using FCMClient in an MVC application to send alerts/messages to Android and iPhones.
I initialise https://github.com/tiagomtotti/firebaseNet/blob/master/src/FirebaseNet/Messaging/Message.cs and then send a message.....
var msg = new Message
{
To = token,
Data = someData
}
This sends alerts to an Android but not an iPhone. I looked at the documentation https://github.com/tiagomtotti/firebaseNet
and can see two classes AndroidNotifcation and IosNotification (https://github.com/tiagomtotti/firebaseNet/tree/master/src/FirebaseNet/Messaging)
I now noticed the Message class takes in an Interface for both notifications but how could setup the Message class to take in a notification but targets both Android and IoS?

Use SmsManager to open the default sms app

I am building a Xamarin Android application (API version>=23) that should call the SMS default application with a prefilled-in number and prefilled-in text.
I figure out how to send the SMS using SmsManager
SmsManager.Default.SendTextMessage("Number", null, "Text", null, null);
The above line send the SMS text directly after the user grants the permission to the app. Is there a way to open the default SMS app instead with prefilled in destination number and text, and let the user press send ?
Any help would be appreciated.
Setting up an Intent can bring up the default SMS massager app:
var intent = new Intent(Intent.ActionSendto);
intent.SetData(Android.Net.Uri.Parse("smsto:" + "555-555-1212"));
intent.PutExtra("sms_body", "StackOverflow rocks");
StartActivity(intent);
It is doubtful that there is no SMS registered app, but you can use the Intent that you populated to check for for which app/activity will launched:
var packagename = intent.ResolveActivity(PackageManager)?.PackageName;

Windows Phone 8 push notification push channel always creates new channel uri

I wanted to check that my push notification implementation is correct.
Each time I open my app (in actual fact I register the push channel only on a specific page so it's each time I go back and forth from that page) a new push channel URI is created which I store in my mobile services database to send push notifications to. This doesn't seem correct to me as each time the app/page is opened a new push channel URI is generated and so the list of channel URIs just grows and grows for each device that uses my app. I'd assume that you create a push channel, store the channel URI and push to it as needed. I will make note here that I am using raw push notifications.
I understand that push channels will expire every so often but for me it's occurring each time I back out of the app/page and therefore when onNavigateTo is called I find the push channel which does exist and a new channel URI is always created. Is this correct?
My code is as follows:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
registerPushChannel();
}
private void registerPushChannel()
{
// The name of our push channel.
string channelName = "RawSampleChannel";
// Try to find the push channel.
pushChannel = HttpNotificationChannel.Find(channelName);
// If the channel was not found, then create a new connection to the push service.
if (pushChannel == null)
{
pushChannel = new HttpNotificationChannel(channelName);
// Register for all the events before attempting to open the channel.
pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);
pushChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(PushChannel_HttpNotificationReceived);
pushChannel.Open();
}
else
{
// The channel was already open, so just register for all the events.
pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);
pushChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(PushChannel_HttpNotificationReceived);
// code which passes the new channel URI back to my web service
}
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
pushChannel.Close();
}
So to clarify, the app is opened and the push channel is registered and the channel uri is saved in my web service. The web service then sends notifications to the channel uri. When I exit the app or page and return to it, the push channel is found but a new channel uri is created which I again save to my web service. My channels table in effect just keeps growing and growing.
So is this the way it should work with new channel URIs continually generated? It kind of doesn't make sense to me. I'm not sure how toast and tile notifications work but I'd assume the channel URI needs to be static when the app closes to keep receiving notifications while the app is closed, but perhaps that could be a functionality of bindtotoast and bindtotile and so what I'm doing is correct because it's to do with raw notifications.
You're mostly doing it right.
Push Notifications are a funny thing.
You create a channel, send it to your server and then the server can send until it fails (the channel Uri expires or there's an error).
At which point the app needs to create a new ChannelUri and then UPDATE the value stored for that app/device on the server. The server will then be able to send notifications.
Some important points
When a new channel Uri is requested for one that is still valid you'll get the same one back.
When your ask for a new channel uri and the current one has expired, you'll normally get the same uri returned but the channel will be made live again.
There is no way to know if a channel has expired from within an app without running code like your registerPushChannel method. (Unless you track this on your backend and the app queries the backend.)
There is no way to tell the app that a channel has expired, or tell the user to reopen the app to re-establish a channel connection using the push infrastructure.
The standard way to try and ensure that the channel is always available is to check the channel whenever the app is started.
This is what you're doing, you probably just want to make sure you're updating server records not just adding more.

Send sms Silently in WP8

I was trying to send sms to a group of contacts via SmsComposeTask class.
But this class's Show() method only composes message in message app. I also want my code to send those texts automatically. Please help!!
Here is what i am using:
SmsComposeTask smsComposeTask = new SmsComposeTask();
smsComposeTask.To = recipients; // recipients is the group of contacts
smsComposeTask.Body =
"Hello! This is a test sms message!";
smsComposeTask.Show();
See: http://social.msdn.microsoft.com/Forums/wpapps/en-us/85dc6ef5-98a9-4a17-a5f0-171372d8d9a3/how-to-send-sms-from-a-windows-phone-page
using Microsoft.Phone.Tasks;
SmsComposeTask smsComposeTask = new SmsComposeTask();
smsComposeTask.To = "2065550123"; smsComposeTask.Body = "Your SMS
text";
smsComposeTask.Show();
After that a dialog will be show asking the
user whether to send the message.
You cannot silently send an SMS
Also: https://developer.nokia.com/Community/Wiki/How_to_send_an_SMS_in_Windows_Phone
Note: In contrast to Symbian, Windows Phone does not allow you to send
sms "directly" or "silently" from your own app. As soon as sms sending
initiation code below is executed the native sms editor app will be
launched, giving the user the option to send the message or not.
I image this is done so that rouge apps can't send out a bunch of texts that will cost someone money.

Sending SMS with delivery message

How can i write code for send SMS with delivery message in Windows Mobile? please guide me with C# or C++ code.
I want to use SmsSendMessage API in this code because this API have more features.
You could use SmsSendMessage to send the message and use SmsGetMessageStatus to get the status report of the sent SMS. This is all C++ code, but you can pinvoke it as well.
Microsoft.WindowsMobile.PocketOutlook Namespace
The Microsoft.WindowsMobile.PocketOutlook namespace provides classes that allow you to create and access PIM data items (Appointments, Tasks, and Contacts), and MAPI messaging items (e-mail and SMS messages), on mobile devices
SmsMessage Class in msdn
This sample shows how to send an SMS message to a mobile phone.
public void SmsMessageSend()
{
SmsMessage smsMessage = new SmsMessage();
//Set the message body and recipient.
smsMessage.Body = "Would you like to meet for lunch?";
smsMessage.To.Add(new Recipient("John Doe", "2065550199"));
smsMessage.RequestDeliveryReport = true;
//Send the SMS message.
smsMessage.Send();
return;
}

Categories