Dave
Dave

Reputation: 5426

Create new Android Notification from app widget in AppWidgetProvider or Service class?

Q) Is it possible to create a system notification (that appears in the pull down shade) from an app widget?

Specifically I want to do this from my:
RemoteFetchService.class, that fetches my remote data... OR
AppWidgetProvider.class, that updates the widget UI.

Upvotes: 1

Views: 1057

Answers (1)

Rosário P. Fernandes
Rosário P. Fernandes

Reputation: 11336

Yes, it is possible to create a notification from a service. Here's an example code.

private void triggerNotification() {
        CharSequence title = "title";
        CharSequence message = "message";
        NotificationManager notificationManager;
        notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification;
        notification = new Notification(
                R.drawable.ico_event, "Notifiy.. ",
                System.currentTimeMillis());
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
        notification.setLatestEventInfo(getApplicationContext(), title, message, pendingIntent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(1, notification);
    }

Have in mind that this is a code I took from my app, so you might need to replace some values to match your app.

Upvotes: 1

Related Questions