Notification with "null" PendingIntent - android-notifications

I'm trying to implement notification in Android.
Now I have a problem, I don't want to have a PendingIntent that user will open any Activity. How can I do that?

PendingIntent contentIntent = PendingIntent.getActivity(
getApplicationContext(),
0,
new Intent(), // add this
PendingIntent.FLAG_UPDATE_CURRENT);

The following works and seems more straightforward:
PendingIntent pi = PendingIntent.getActivity(context, 0, null, 0);
Having a notification without launching a subsequent Activity seems quite sensible to me - eg "Its time to get up!.

Related

Use auto Flashlight while scanning QR Code with ZXing in Xamarin Forms

I am trying to scan a QR code with my xamarin forms app
I use the ZXing Package
I was able to turn the flashlight on the whole time with this:
TimeSpan timespan = new TimeSpan(0, 0, 0, 0, 0);
Device.StartTimer(timespan, () =>
{
scanner.Torch(true);
return true;
});
var scanResult = await scanner.Scan(optionsCustom);
return scanResult.Text;
Is there a way to turn the flashlight on only when needed?
or intercept the Volume Buttons to turn the Flashlight on and off manually
You can create custom overlay for your scanner with button that turns on/off the flashlight.

Save bitmap to gallery and access it through local notification

In an app, I am taking a screenshot of the content of a page, and I want to save it in the Gallery, create a local notification with the image in it, and when the user taps on it, it open the image from the Gallery.
As of now, I am able to take the snapshot and save it using this :
var path = MediaStore.Images.Media.InsertImage(context.ContentResolver, bitmap, "TestImage.png", "Snapshot taken");
I have a few problems with this approach, I can see the image in the Pictures folders from the Gallery, but the name is not the one I specified in this case "TestImage.png". Also, the path returned from this call has the URI scheme "content://...".
After that, I create the local notification with NotificationCompat.Builder with the embedded image, it works well. But when I try to add the PendingIntent to it to open the image I have an issue with the path returned from InsertImage call. The path returned is :
content://media/external/images/media/1292
I tried this :
var path = FileProvider.GetUriForFile(CrossCurrentActivity.Current.AppContext, "com.myapp.fileprovider", new File(path));
but it doesn't work, I get an exception :
Java.Lang.IllegalArgumentException: Failed to find configured root that contains /content:/media/external/images/media/1292
I also tried this :
global::Android.Net.Uri.FromFile(new File(path)), "image/*")
but I receive another exception :
Android.OS.FileUriExposedException: file:///content%3A/media/external/images/media/1292 exposed beyond app through Intent.getData()
This is the code I use to create the notification:
NotificationCompat.BigPictureStyle picStyle = new NotificationCompat.BigPictureStyle();
picStyle.BigPicture(bitmap);
picStyle.SetSummaryText("The snapshot has been saved to your library");
const int pendingIntentId = 0;
PendingIntent pendingIntent =
PendingIntent.GetActivity(CrossCurrentActivity.Current.Activity, pendingIntentId,
new Intent().SetAction(Intent.ActionView)
.SetDataAndType(global::Android.Net.Uri.FromFile(new File(path)), "image/*"), PendingIntentFlags.OneShot);
NotificationCompat.Builder builder = new NotificationCompat.Builder(CrossCurrentActivity.Current.AppContext, "UniqueID")
.SetContentIntent(pendingIntent)
.SetContentTitle("Security center")
.SetContentText("The snapshot has been saved to your library")
.SetSmallIcon(Resource.Drawable.ic_notification);
builder.SetStyle(picStyle);
Notification notification = builder.Build();
NotificationManager notificationManager =
CrossCurrentActivity.Current.Activity.GetSystemService(Context.NotificationService) as NotificationManager;
const int notificationId = 0;
notificationManager.Notify(notificationId, notification);
Do you guys have any idea on how I could save the image with the right name in the Public Gallery folder then access it in the PendingIntent for the local notification ?
Thank you all !
Okay so after a few hours of trial and error, I found out the issue, when parsing the path returned from the MediaStore.Images.Media.InsertImage, I had to use global::Android.Net.Uri.Parse() and not Uri.FromFile(), the latter is only if your path has the URI scheme "file://". By doing that I was able to make the ContentIntent work on the local notification. Only problem now is the name of the file that's not respected.

Xamarin.Auth Google not auto close when done login

I follow guide enter link description here
I have a issue when I done login my google account it show toast
And browser not auto close to back my.
Thanks!
In you CustomUrlSchemeInterceptorActivity page replace inside OnCreate.
base.OnCreate(savedInstanceState);
global::Android.Net.Uri uri_android = Intent.Data;
Uri uri_netfx = new Uri(uri_android.ToString());
// load redirect_url Page
AuthenticationState.Authenticator.OnPageLoading(uri_netfx);
var intent = new Intent(this, typeof(MainActivity));
intent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
StartActivity(intent);
this.Finish();
return;
This is the warning message shown in the toast.
We can make it null by adding a piece of code in MainActivity just after initializing Xamarin.Auth.
CustomTabsConfiguration.CustomTabsClosingMessage = null;
And for closing the CustomTab login screen and navigating back to your app, set the LaunchMode of your Custom URL activity to SingleTask.
LaunchMode = LaunchMode.SingleTask
Hope this will help.
Worked for me as well in avoiding the toast:
global::Xamarin.Auth.CustomTabsConfiguration.CustomTabsClosingMessage = null

Android Notification not fired with no small icon or has no message if small icon set

I'm creating a notification as per the code below. When I run it all I get across the top of the device (avd) is the small icon with no text (I'd have thought 'From Neil' to be displayed) but the notification is full and complete when I drag down to show the notification page.
However, if I comment out the setSmallIcon the notification isn't even created and nothing happens. What's going wrong?
Thanks.
private void doNotification() {
NotificationCompat.Builder b = new NotificationCompat.Builder(this);
b.setContentTitle("From Neil");
b.setContentText("Hello email from neil");
b.setSmallIcon(android.R.drawable.ic_dialog_alert);
//do not want an intent fired
pIntent = PendingIntent.getActivity(this, 0, new Intent(),
PendingIntent.FLAG_UPDATE_CURRENT);
b.setContentIntent(pIntent);
Notification n=b.build();
n.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, n);
}
What you're seeing is correct behavior: The status bar simply adds your small icon to the notification area. You're probably thinking of the tickerText, which if set will cause some text to scroll by when the notification is first posted. Call setTicker("From Neil"), or perhaps more informatively "Neil: Hello email from neil" on your Notification.Builder.

Change Android default notification sound based on time

I was wondering if it is possible to programmatically change the default notification sound on an Android phone based on what time it is. For instance, I would like the default notification sound to change to Silent after 11PM and then back to a sound at 8AM. I have a working knowledge of Java, but have never done any Android development. Thanks in advance for any help.
Without going too much into detail, I would approach this problem by first creating an AlarmService which will run periodically and change default Ringtone according to your desired time of the day via RingtoneManager
AlarmManager setting up periodical invocations of "RingToneChanger.class" , something like this:
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, Alarm.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 10, pi); // Millisec * Second * Minute
Then within triggered RingToneChanger.class, ringtone can be changed like this:
if(isNoon) {
RingtoneManager.setActualDefaultRingtoneUri(Context context, RingtoneManager. TYPE_NOTIFICATION , Uri lunchtimeNotificationSound);
} else {
RingtoneManager.setActualDefaultRingtoneUri(Context context, RingtoneManager. TYPE_NOTIFICATION , Uri defaultNotificationsound);
}
Hope it helps. else please don't hesitate to ask.

Resources