Notification without "Open" action - wear-os

I'm creating a notification that has a on content click action that opens the activity that i want it to open.
Intent myIntent = new Intent(this, WearRunActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, myIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.card_icon)
.setContentTitle("Running...")
.setContentText(chronometer.getText())
.setLargeIcon(BitmapFactory.decodeResource(
getResources(), R.drawable.card_background_blur))
.setAutoCancel(true)
.setOngoing(true)
.setUsesChronometer(true)
.setWhen(initialTime)
.setContentIntent(pendingIntent)
.extend(new NotificationCompat.WearableExtender()
.addAction(new NotificationCompat.Action.Builder(R.drawable.start_icon, "abrir", pendingIntent).build())
.setContentAction(0));
NotificationManagerCompat notification_manager = NotificationManagerCompat.from(this);
notification_manager.notify(1, notificationBuilder.build());
Right now, my notification has 3 "views":
the content
"Open" buttton
"Block app" button
How can I delete the "Open" button on the notification in order to have only 2 "views", the content and the "block app"?

Since you are running this code on your watch, you can comment out the line
.setContentIntent(pendingIntent)
and see if that gives you what you want.

Related

Nativescript - How to bring app to foreground when notification is pressed

I'm writing a native Android code to open my app when a notification is pressed. If the app is already opened (whether running in foreground or in background), I want clicking the notification to bring the app to front, without restarting it, so that its state is preserved.
I tried the following code (showing only relevant code):
///////// Create an activity on tap (intent)
const Intent = android.content.Intent;
const PendingIntent = android.app.PendingIntent;
// Create an explicit intent for an Activity in your app
const intent = new Intent(context, com.tns.NativeScriptActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_NEW_TASK);
const pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
///////// Creating a notification
var NotificationCompat = android.support.v4.app.NotificationCompat;
const builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(android.R.drawable.btn_star_big_on)
.setContentTitle(title)
.setContentText(message)
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText("By default, the notification's text content is truncated to fit one line.")
)
.setPriority(NotificationCompat.PRIORITY_HIGH)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setAutoCancel(true);
///////// Show the notification
notificationManager.notify(NOTIFICATION_ID, builder.build());
But that opened the application without preserving its state.
Following recommendations here, I also tried emulating pressing the app icon from the launcher - so that the app is just brought to the frontground and the Nativescript activity is not recreated.
const packageName = context.getPackageName();
console.log('Package name: ',packageName);
const emulateLaunchByAppIconIntent = context.getPackageManager()
.getLaunchIntentForPackage(packageName)
.setPackage(null)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
const pendingIntent_emulated = PendingIntent.getActivity(context, 0, emulateLaunchByAppIconIntent, 0);
///////// Creating a notification
var NotificationCompat = android.support.v4.app.NotificationCompat;
const builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(android.R.drawable.btn_star_big_on)
.setContentTitle(title)
.setContentText(message)
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText("By default, the notification's text content is truncated to fit one line.")
)
.setPriority(NotificationCompat.PRIORITY_HIGH)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent_emulated)
.setAutoCancel(true);
///////// Show the notification
notificationManager.notify(NOTIFICATION_ID, builder.build());
This indeed made the app come to front, but didn't preserve its state (even if the app was already in foreground - it reloaded the app).
Then I tried pressing a Nativescript application app icon (manually), when the app has just been sent to the background - and I found that it would restart the app, and not just bring it to the foreground.
My question is - why does a Nativescript application behave like this?
How can I make Android just bring the app to foreground and not re-build a new nativescript activity?
The code in the question above, actually works - and does bring the app to the foreground without restarting the app, preserving its state.
The reason the app was restarting, even by clicking the Nativescript application app icon (manually) was related to the development environment.
This happened when I was running the app on a physical device while connected to my Mac running tns run android --bundle, or
Running the app in an emulator (either by running tns run android --bundle or by launching the app directly from the app icon)
Running the app on a physical device that is not connected to the nativescript development environment - showed the real behavior - and the app was brought to the foreground without restarting after clicking the notification.
More info with code examples:
I found that there's no need to launch the app by simulating an icon press using this code
const emulateLaunchByAppIconIntent = context.getPackageManager()
.getLaunchIntentForPackage(packageName)
.setPackage(null)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
const pendingIntent_emulated = PendingIntent.getActivity(context, 0, emulateLaunchByAppIconIntent, 0);
as PendingIntent.FLAG_UPDATE_CURRENT or Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED in the code below, is sufficient to bring app to the foreground without restarting after clicking the notification:
const openActivityIntent = new Intent(context, com.tns.NativeScriptActivity.class);
openActivityIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_NEW_TASK);
const openActivityPendingIntent = PendingIntent.getActivity(context, 0, openActivityIntent, 0);
///////// Creating a notification
const notificationManager = <NotificationManager> context.getSystemService(NotificationManager.class);
var NotificationCompat = android.support.v4.app.NotificationCompat;
const builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSound(soundUri)
.setSmallIcon(android.R.drawable.ic_lock_idle_alarm)
.setContentTitle(title)
.setContentText(message)
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText('More explaination text if needed. Disabled for now.')
)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Set the intent that will fire when the user taps the notification
.setContentIntent(openActivityPendingIntent)
.setWhen(scheduledTime)
.setAutoCancel(true)
.build();
///////// Show the notification
notificationManager.notify(NOTIFICATION_ID, builder);
Hope this helps you if you got into the same issue.

Open a New Frame Window From MainPage in Windows 10 Universal App?

How to open a new frame window which contains another xaml page when I click a button from the main window in universal app?
You can check out a sample on how to do this from the Offical Microsoft Samples on GitHub as can be found here but I'll summarize quickly here. Another simpler implementation can be found on Mike Taulty's Blog.
Since you haven't specified a development language I will assume C# and XAML.
Create your button in XAML which will be clicked to create a new window:
<Button
Content="Create"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Click="OnCreate" />
In the code behind, add your OnCreate click handler:
async void OnCreate(object sender, RoutedEventArgs e)
{
CoreApplicationView newCoreView = CoreApplication.CreateNewView();
ApplicationView newAppView = null;
int mainViewId = ApplicationView.GetApplicationViewIdForWindow(
CoreApplication.MainView.CoreWindow);
await newCoreView.Dispatcher.RunAsync(
CoreDispatcherPriority.Normal,
() =>
{
newAppView = ApplicationView.GetForCurrentView();
Window.Current.Content = new SubWindowUserControl();
Window.Current.Activate();
});
await ApplicationViewSwitcher.TryShowAsStandaloneAsync(
newAppView.Id,
ViewSizePreference.UseHalf,
mainViewId,
ViewSizePreference.UseHalf);
}
This creates a new window, and fetches the application view ID of your original window for you to reference. It then awaits a dispatched thread to run (in the UI thread of the new window) to get the new view the window contains which it adds a new SubWindowUserControl to, programatically, and is then activated (you MUST remember to do this). The new window is then shown in a new standalone window using some standard parameters.
Check out the API documentation here for more details on the ApplicationViewSwitcher class.
To take this code and make it show a new XAML page you can create your new page and change the async task running on the new window in the OnCreate code as follows:
await newCoreView.Dispatcher.RunAsync(
CoreDispatcherPriority.Normal,
() =>
{
newAppView = ApplicationView.GetForCurrentView();
Window.Current.Content = new Frame();
(Window.Current.Content as Frame).Navigate(typeof(<your_page>));
Window.Current.Activate();
});
This, instead of showing a new custom XAML element as its content, creates a new Frame which is then navigated to show your XAML page.
Hope this helps!

Xamarin UIAlertView clicked - Opens new View

just wondering basically i have an Azure authentication system that opens when clicking on a facebook button or twitter button it then asks to authenticate the app and once logged in displays a UIalertview with the options to click "OK" or "Cancel".
I was wondering how once they clicked ok i could get it to display the next View?
I know my uialertview is called alert - so was thinking it would alert.Clicked (); then something in there but not sure what.
Here is the method that is processing the login and the alertview if someone can get back to me fast.
private void DoLogin(MobileServiceAuthenticationProvider provider)
{
var task = this.client.LoginAsync(this, provider).ContinueWith(t => {
MobileServiceUser user = t.Result;
this.BeginInvokeOnMainThread(() => {
UIAlertView alert = new UIAlertView("Logged In!", string.Format ("Hello user {0}", user.UserId),
null, "OK", new string[] {"Cancel"});
alert.Clicked();
alert.Show ();
});
});
}
Thanks
For example you could do this:
alert.Clicked += (sender, args) =>
{
// check if the user NOT pressed the cancel button
if (args.ButtonIndex != alert.CancelButtonIndex)
{
// present your next UIViewController, something like this
NavigationController.PushViewController(new YourNextViewController(), true);
}
};
For more information about UIAlertView check out it's documentation:
https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIAlertView_Class/index.html

The simplest way to show a toast notification in WP7?

I've been looking for a way to show an offline (not pushed from a server) toast notification on WP7 for a while now. Any ideas?
If you're trying to show the notification while your application is running, you can use the ToastPrompt from the coding4fun toolkit: http://coding4fun.codeplex.com/
If you want to show the notification while your app isn't running, you can use the ShellToast class from a background agent: http://msdn.microsoft.com/en-us/library/microsoft.phone.shell.shelltoast(v=vs.92).aspx
Within a background task, simply call this method:
public static void makeToast(string toastText)
{
//Make a new toast with content "text"
ShellToast toast = new ShellToast();
toast.Title = "Toast Title: ";
toast.Content = toastText;
//toast.NavigationUri = new Uri();
toast.Show();
}

how to close the CameraCaptureDialog dialog automatically after taking a photo using C#

I am doing a work that scans the barcode and gets the information behind it under windows mobile 6.5 and .net compact framework 2.0, using CameraCaptureDialog class to capture a picture from the phone camera, this is my code:
CameraCaptureDialog cameraCaptureDialog = new CameraCaptureDialog();
cameraCaptureDialog.Owner = this;
cameraCaptureDialog.Title = "";
cameraCaptureDialog.Mode = CameraCaptureMode.Still;
cameraCaptureDialog.InitialDirectory = dir;
cameraCaptureDialog.Resolution = new System.Drawing.Size(640, 480);
cameraCaptureDialog.DefaultFileName = "CodeBar.jpg";
if (cameraCaptureDialog.ShowDialog() == DialogResult.OK && cameraCaptureDialog.FileName.Length > 0)
{
curFileName = cameraCaptureDialog.FileName;
cameraCaptureDialog.Dispose();
}
//deal with CodeBar.jpg
the question is, during my code running, I have to press the capture button on the cameraCaptureDialog, then press the back button to return to my program to deal with the captured file, this operation needs to press 2 buttons, for user convenience, I want the cameraCaptureDialog to close automatically after the user pressed the capture button, how can this be done? as I know, when ShowDialog() method is called, cameraCaptureDialog won't return until the back button shown on the dialog, so I could do nothing before manually close the cameraCaptureDialog and the ShowDialog() method returned DialogResult.OK

Resources