Xamarin forms open view model on notification tap - xamarin.android

I am using this code to show notification in notification bar. When the notification is tapped, main activity is launched. Is it possible to launch the view model instead of activity in Xamarin forms app with MvvmCross.
Intent notificationIntent = new Intent(context,typeof(MainActivity));
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pIntent = PendingIntent.getActivity(context, code,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager manager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notify = new NotificationCompat.Builder(
context);
notify.setContentIntent(pIntent);
notify.setSmallIcon(R.drawable.app_icon);
notify.setContentTitle(“Title”);
manager.notify(reqCode, notify.build());

My idea was to make use of PutExtra in combination with MessagingCenter.
First, you show the notification in the notification bar:
Intent intent = new Intent(Forms.Context, typeof(MainActivity));
if (openPage)
{
intent.SetFlags(ActivityFlags.SingleTop);
intent.PutExtra("OpenPage", "SomePage");
}
const int pendingIntentId = 0;
PendingIntent pendingIntent = PendingIntent.GetActivity(Forms.Context, pendingIntentId, intent, PendingIntentFlags.OneShot);
var nMgr = (NotificationManager)Android.App.Application.Context.GetSystemService(Context.NotificationService);
Notification.Builder notBuilder = new Notification.Builder(Android.App.Application.Context)
.SetContentIntent(pendingIntent)
.SetContentTitle("SomeApp")
.SetContentText(message)
.SetDefaults(NotificationDefaults.Sound | NotificationDefaults.Vibrate)
.SetSmallIcon(Resource.Drawable.ic_launcher)
.SetAutoCancel(true);
var notification = notBuilder.Build();
nMgr.Notify(0, notification);
In MainActivity.cs you check for the extra content:
protected override void OnNewIntent(Intent intent)
{
// Send message to the PCL (XF) if a certain page should be opened.
if (intent.HasExtra("OpenPage"))
{
string pageName = intent.GetStringExtra("OpenPage") ?? "None";
if (pageName != "None")
{
var message = new OpenPageMessage { PageName = pageName };
MessagingCenter.Send(message, Message.Msg_OpenPage);
}
}
base.OnNewIntent(intent);
}
And your central navigation instance (e.g. MainPage), subscribes to this message:
MessagingCenter.Subscribe<Message.OpenPageMessage>(this, Message.Msg_OpenPage, (async) message =>
{
// Loads a certain page if a message is received
switch (message.PageName)
{
case "SomePage":
await Navigation.PushModalAsync(new SomePage(), true);
break;
default:
break;
}
});
Additionally, here is my Message.cs:
public class Message
{
public const string Msg_OpenPage = "OpenPage";
public class OpenPageMessage {
public string PageName { get; set; }
}
}
With the help of this source.
Edit
There are issues if you have multiple push notifications at a time, where notfications where overwritten. One could use a different requestCode or use FLAG_UPDATE_CURRENT.

Related

SqlDependency not working mvc app. Hits once

I am following the blog http://www.venkatbaggu.com/signalr-database-update-notifications-asp-net-mvc-usiing-sql-dependency/ to get a signalR push message out to connected clients.
My debugger never hits the onchange event.
my Global.asax.cs:
string connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
protected void Application_Start()
{
// basic stuff
SqlDependency.Start(connString);
var repo = new Repositories.MarkerRepository();
repo.GetAllMarkers(); // to register the dependency
}
My MarkerRepository.cs:
readonly string _connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
private MarkersHub _mh = new MarkersHub(); // my signalr hub class
public IEnumerable<House> GetAllMarkers()
{
var markers = new List<House>();
using (var connection = new SqlConnection(_connString))
{
connection.Open();
using (var command = new SqlCommand(#"SELECT * FROM [dbo].Houses", connection))
{
command.Notification = null;
var dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
var reader = command.ExecuteReader();
while (reader.Read())
{
markers.Add(item: new House {
ID = (int)reader["ID"],
Name = (string)reader["Name"],
Code = reader["Code"] != DBNull.Value ? (string)reader["Code"] : "",
Latitude = Convert.ToDouble(reader["Latitude"]),
Longitude = Convert.ToDouble(reader["Longitude"])
});
}
}
}
return markers;
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change)
{
_mh.SendMarkers();
}
}
I have had a hit once but it was no change, only a notification for subscribe. I have read a lot about resubscribe, but when it hit this event the sql:
select * from sys.dm_qn_subscriptions
still returns no rows. Not on my db or master. So I think that there is an error in the blog post with the re-subscribe to the on change event? This sample https://msdn.microsoft.com/en-us/library/a52dhwx7(VS.80).aspx does unregister in the onchange event and calls the method which registers a new event. Can someone verify my assumption?
These were the values for the SqlNotificationEventArgs e in my event and told me that my query to depend on, was invalid.
SqlNotificationEventArgs.Type --> SqlNotificationType.Subscribe
SqlNotificationEventArgs.Info --> SqlNotificationInfo.Invalid
SqlNotificationEventArgs.Source --> SqlNotificationSource.Statement
The statement may not use the asterisk () or table_name. syntax to specify columns.
source https://msdn.microsoft.com/en-us/library/ms181122.aspx

Auto OTP verification android xamarin

I am working on an Android App using Xamarin, in which server sends an OTP and the user needs to enter this OTP in the App, to SignUp for my App. What I want is, that my App should be able to automatically read the OTP sent by the server and to be filled in edit text field of OTP.
I'm almost done to read the message but unable to set the otp in edit text field.
SMS broadcast receiver class:
[BroadcastReceiver(Enabled = true, Label = "SMS Receiver")]
[IntentFilter(new string[] { "android.provider.Telephony.SMS_RECEIVED" })]
public class SMSBroadcastReceiver : BroadcastReceiver
{
private const string IntentAction = "android.provider.Telephony.SMS_RECEIVED";
public override void OnReceive(Context context, Intent intent)
{
try
{
if (intent.Action != IntentAction) return;
var bundle = intent.Extras;
if (bundle == null) return;
var pdus = bundle.Get("pdus");
// var castedPdus = JNIEnv.GetArray(pdus.Handle);
var castedPdus = JNIEnv.GetArray<Java.Lang.Object>(pdus.Handle);
var msgs = new SmsMessage[castedPdus.Length];
var sb = new StringBuilder();
string sender = null;
for (var i = 0; i < msgs.Length; i++)
{
var bytes = new byte[JNIEnv.GetArrayLength(castedPdus[i].Handle)];
JNIEnv.CopyArray(castedPdus[i].Handle, bytes);
string format = bundle.GetString("format");
msgs[i] = SmsMessage.CreateFromPdu(bytes,format);
if (sender == null)
sender = msgs[i].OriginatingAddress;
sb.Append(string.Format("SMS From: {0}{1}Body: {2}{1}", msgs[i].OriginatingAddress,System.Environment.NewLine, msgs[i].MessageBody));
Toast.MakeText(context, sb.ToString(), ToastLength.Long).Show();
}
}
catch (System.Exception ex)
{
Toast.MakeText(context, ex.Message, ToastLength.Long).Show();
}
}
}
Here is my main activity:
[Activity(Label = "UserSms", MainLauncher = true, Icon = "#drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
SMSBroadcastReceiver smsReceiver = new SMSBroadcastReceiver();
TextView msg = FindViewById<TextView>(Resource.Id.editTextOtp);
Button btn = FindViewById<Button>(Resource.Id.button3);
RegisterReceiver(smsReceiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
}
}
How can I achieve this? Any help or guidance in this regard would be highly appreciated.
Update
public void onSMSReceived(string msgs)
{
EditText OtpNumber = (EditText)FindViewById(Resource.Id.editTextOtp);
try
{
OtpNumber.SetText(msgs.ToString(),null);
}
catch (System.Exception ex)
{
}
}
Your are on the finishing line. You only need to do these thing:
Create an interface which will have public method onSMSReceived(String smsMsg)
Instantiate that interface.
Implement that interface in MainActivity activity.
Override onSMSReceived(String smsMsg) in your MainActivity
Notify MainActivity using above created interface from your SMS Broadcast Receiver.
Populate message received in onSMSReceived(String smsMsg) in your MainActivity.
You are done.
I didn't get exactly how you're doing it, but i did in two ways,
1.User has to enter it manually,
2.We have to read automatically through the programming,
But i faced one problem in reading sms automatically, like sending sms and reading sms are calling at the same time may be like register click event, I found one more way to detect automatically like sending otps two times and storing generated otps in a list of string and comparing with message.body
Here the problem is we have to send otp two times, still i'm figuring out how to call reading sms part after sometime,,,!
If you want that solution plz mail me at sailokeshgoud#gmail.com

How to get application process id that send SMS in BlackBerry

I'm looking for the way to get process id of the application which send SMS. I can get the content of sending message with OutboundMessageListener but I don't know how to get the process id.
Here is my try:
MessageConnection _mc = (MessageConnection) Connector.open("sms://:0");
_mc.setMessageListener(new OutboundSMSListener());
private static final class OutboundSMSListener implements OutboundMessageListener
{
public void notifyIncomingMessage(MessageConnection messageconnection)
{
System.out.println("Incoming message received ");
}
public void notifyOutgoingMessage(Message message)
{
System.out.println("------------------------\n\n\n\n\n");
System.out.println("Message send: " + message);
}
}
Is there any way to do this?
Is the app sending SMS a native app of the BlackBerry device, and does it reside in the foreground?
For getting a foreground application's process id you can use the following code:
private int getForegroungProcessID() {
return ApplicationManager.getApplicationManager().getForegroundProcessId();
}
private String getAppNameByProcessId(int id) {
String result = null;
ApplicationManager appManager = ApplicationManager.getApplicationManager();
ApplicationDescriptor appDes[] = appManager.getVisibleApplications();
for (int i = 0; i < appDes.length; i++) {
if (appManager.getProcessId(appDes[i]) == id) {
result = appDes[i].getModuleName();
//// here check the app name...is it a messaging app ///
break;
}
}
return result;
}

Launch application on clicking message in Messagelist of Blackberry

I am using a custom folder in the Messagelist to display when my application gets new message.
Each message contains the title "New information received".
Right now, when the message is clicked it opens a new email compose window.
I have overridden ApplicationMessageFolderListener, but actionPerformed does not seem to be invoked when the message is clicked.
I want to launch my application when the user goes into the Messages application and clicks on my custom message. How do I achieve this?
Target platform: Blackberry 4.0 and above.
use the following code:
static class OpenContextMenu extends ApplicationMenuItem {
public OpenContextMenu( int order ) {
super( order );
}
public Object run( Object context ) {
if( context instanceof NewMessage ) {
try {
NewMessage message = (NewMessage) context;
if( message.isNew() ) {
message.markRead();
ApplicationMessageFolderRegistry reg = ApplicationMessageFolderRegistry.getInstance();
ApplicationMessageFolder folder = reg.getApplicationFolder( MessageList.INBOX_FOLDER_ID );
folder.fireElementUpdated( message, message );
//changeIndicator(-1);
}
Inbox inbox = message.getInbox();
Template template = inbox.getTemplate();
//Launch the mainscreen
UiApplication.getUiApplication().requestForeground();
}
catch (Exception ex) {
Dialog.alert();
}
}
return context;
}
public String toString() {
return "Name of the menu item";
}
}

Create an application which will lock another application event

Actually I want to make an application which will getGlobalEvent and control that event through another custom application. Is there any way to do so. Can i get global event from a particular application? Its like an application which will lock custom application in your blackberry, if you add following application in that locking app list and put password to access then when u try to open that application, it will ask for a password which u set in the locking app.
Common advices
this should be background application
in timer thread check current foreground application
use custom global modal dialog to request password
if password was wrong close app by simulating back key press or move app to background
Checking Application
Have to say, there can be several processes within one application so we will perform check based on module name:
private String getModuleNameByProcessId(int id) {
String result = null;
ApplicationManager appMan = ApplicationManager.getApplicationManager();
ApplicationDescriptor appDes[] = appMan.getVisibleApplications();
for (int i = 0; i < appDes.length; i++) {
if (appMan.getProcessId(appDes[i]) == id) {
result = appDes[i].getModuleName();
break;
}
}
return result;
}
Move application to Background?
Yep, there's no requestBackground() in ApplicationManager... so what you can do is requestForeground() on the next best app which is not on foreground, and this will move active app to background! You can even bring up Home Screen with requestForegroundForConsole():
protected int switchForegroundModule() {
int id = -1;
ApplicationManager appMan = ApplicationManager.getApplicationManager();
ApplicationDescriptor appDes[] = appMan.getVisibleApplications();
for (int i = 0; i < appDes.length; i++) {
if (!appDes[i].getModuleName().equalsIgnoreCase(STR_MODULE_NAME)) {
id = appMan.getProcessId(appDes[i]);
appMan.requestForeground(id);
// give a time to foreground application
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
}
return id;
}
Global Dialog
Just to input password you can extend Dialog, it will be easier to consume result:
class PaswordDialog extends Dialog {
private BasicEditField mPwdField = new BasicEditField();
public PaswordDialog() {
super(Dialog.D_OK_CANCEL, "Enter password", Dialog.CANCEL, null,
Dialog.FIELD_HCENTER);
add(mPwdField);
}
public String getPassword() {
return mPwdField.getText();
}
}
And password check will look like:
private boolean checkPassword() {
boolean result = false;
final PaswordDialog pwdDlg = new PaswordDialog();
invokeAndWait(new Runnable() {
public void run() {
Ui.getUiEngine().pushGlobalScreen(pwdDlg, 0,
UiEngine.GLOBAL_MODAL);
}
});
result = ((Dialog.OK == pwdDlg.getSelectedValue()) && pwdDlg
.getPassword().equalsIgnoreCase(STR_PASSWORD));
return result;
}
Put this all together
Sample to block Adress Book App:
public class LockMainApp extends Application {
private static final String STR_MODULE_NAME = "net_rim_bb_addressbook_app";
private static final String STR_PASSWORD = "12345";
int mFGProcessId = -1;
public LockMainApp() {
Timer timer = new Timer();
timer.schedule(mCheckForeground, 1000, 1000);
}
public static void main(String[] args) {
LockMainApp app = new LockMainApp();
app.enterEventDispatcher();
}
TimerTask mCheckForeground = new TimerTask() {
public void run() {
int id = ApplicationManager
.getApplicationManager().getForegroundProcessId();
if (id != mFGProcessId) {
mFGProcessId= id;
String moduleName = getModuleNameByProcessId(mFGProcessId);
if (moduleName.equalsIgnoreCase(STR_MODULE_NAME)) {
if (!checkPassword())
mFGProcessId = switchForegroundModule();
}
}
};
};
}

Resources