Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to activate an activity using a local notification created from a remote notification

I have successfully created a local notification that starts an activity, but for some reason when this local notification is created from within the handler of a remote notification the activity is not started when the local notification is tapped by the user. No error or exception seems to be thrown.

Below is the code that creates the local notification. Note I'm using Xamarin. I wonder if it is perhaps somehow permissions related (remote notification handlers maybe cannot create intents to start activities?).

private void CreateNotification(string title, string desc) {
    var uiIntent = new Intent(this, typeof(ConversationActivity));

    var stackBuilder = TaskStackBuilder.Create(this);
    stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(ConversationActivity)));
    stackBuilder.AddNextIntent(uiIntent);

    PendingIntent pendingIntent = stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);

    var notification = new NotificationCompat.Builder(this)
        .SetAutoCancel(true) // Remove the notification once the user touches it
        .SetContentIntent(pendingIntent)
        .SetContentTitle(title)
        .SetSmallIcon(Resource.Drawable.AppIcon)
        .SetContentText(desc)
        .SetDefaults((int)(NotificationDefaults.Sound | NotificationDefaults.Vibrate))
        ;

    // Set the notification info
    // we use the pending intent, passing our ui intent over which will get called
    // when the notification is tapped.
    var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
    notificationManager.Notify(1, notification.Build());
}
like image 927
Andrew Arnott Avatar asked Dec 26 '13 23:12

Andrew Arnott


People also ask

How do I start an activity from notifications?

Build and issue the notification: Create an Intent that starts the Activity . Set the Activity to start in a new, empty task by calling setFlags() with the flags FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK . Create a PendingIntent by calling getActivity() .

What is remote notification in IOS?

Overview. Use remote notifications (also known as push notifications) to push small amounts of data to devices that use your app, even when your app isn't running. Apps use notifications to provide important information to users. For example, a messaging service sends remote notifications when new messages arrive.

Can you view notification history on Android?

Enable and check notification history on AndroidSwipe up from the homescreen to open the app drawer menu. Find and open the Settings app (it looks like a gear icon). Open Notifications. Select Notification history.


1 Answers

I'm still not sure what was wrong with my original attempt, but I did find out I could fix it by changing the Intent to use a component name instead of an action or activity type:

    private void SendNotification() {
        var nMgr = (NotificationManager)this.GetSystemService(NotificationService);
        var notification = new Notification(Resource.Drawable.AppIcon, "Incoming Dart");
        var intent = new Intent();
        intent.SetComponent(new ComponentName(this, "dart.androidapp.ContactsActivity"));
        var pendingIntent = PendingIntent.GetActivity(this, 0, intent, 0);
        notification.SetLatestEventInfo(this, "You've got something to read", "You have received a message", pendingIntent);
        nMgr.Notify(0, notification);
    }
like image 66
Andrew Arnott Avatar answered Oct 07 '22 15:10

Andrew Arnott