[Android] How to create notifications, even when the app is closed.(2)

[REFERENCE LINK] https://forum.processing.org/two/discussion/4429/notifications-in-android-mode https://forum.processing.org/two/discussion/16840/how-to-create-notifications-even-when-the-app-is-closed

How to create notifications, even when the app is closed.

Is it impossible with the Processing(android mode) program?

https://developer.android.com/guide/topics/ui/notifiers/notifications.html#CreateNotification

[EXAM]

import android.view.MotionEvent; 
import android.content.Context;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Activity;

Activity act;
NotificationManager gNotificationManager;
Notification gNotification;
long[] gVibrate = {0,250,50,125,50,62};
Context ctx;

void setup() {
  size(displayWidth, displayHeight);


}

void draw() {

}


void onResume() {
  super.onResume();
  // Create our Notification Manager:
  gNotificationManager = (NotificationManager) this.getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
  // Create our Notification that will do the vibration:
  gNotification = new Notification();
  // Set the vibration:
  gNotification.vibrate = gVibrate;
}

public boolean surfaceTouchEvent(MotionEvent event) {
  // If user touches the screen, trigger vibration notification:
  gNotificationManager.notify(1, gNotification);
  return super.surfaceTouchEvent(event);
}

It does not work.

Answers

  • edited April 2018

    @Gwak===

    if your question was= how to make a notif that is displayed when the app is closed (on pause), the answer is yes of course. in your app you have to create the notification;

    • for that you use an instance of Notification.Builder, adding to it what you want (title, text message....) and an icon (usually that is your app icon, but you can use another one). You can also add an intent (pending intent) as an action which is executed if the user click your notification.

    • then you create an instance of NotificationManager and use its method: notify() with an ID (some integer) for your notification and your Notification.Builder.build() as arguments.

    • Finally you have only to define some action for notifying, some button, mouseReleased(), what you want. Now, if you want that the action be external (like an alarm) you can use a broadcastReceiver : when the event is triggered it calls the Notification from your app.

    • All these things are easy to do; yet with processing you have 1) to adapt the code for a fragment 2) you cannot set the icon only with .setSmallIcon(R.drawable.myIcon), you have to create it as a bitmap then code .setSmallIcon(Icon.createWithBitmap(bitmap)); i have tested: it works...

Sign In or Register to comment.