mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-11-25 22:39:22 +00:00
Display notifications
This commit is contained in:
parent
2857440d0c
commit
0c75575a70
@ -9,12 +9,15 @@ import android.content.Context;
|
|||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
|
import android.os.Handler;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
|
import android.os.Looper;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.core.app.NotificationCompat;
|
import androidx.core.app.NotificationCompat;
|
||||||
|
import androidx.core.app.NotificationManagerCompat;
|
||||||
import androidx.core.content.ContextCompat;
|
import androidx.core.content.ContextCompat;
|
||||||
|
|
||||||
import com.neovisionaries.ws.client.WebSocket;
|
import com.neovisionaries.ws.client.WebSocket;
|
||||||
@ -25,6 +28,7 @@ import com.neovisionaries.ws.client.WebSocketFrame;
|
|||||||
|
|
||||||
import org.communiquons.comunic.MainActivity;
|
import org.communiquons.comunic.MainActivity;
|
||||||
import org.communiquons.comunic.R;
|
import org.communiquons.comunic.R;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -32,10 +36,13 @@ import java.util.Map;
|
|||||||
public class NotificationsService extends Service implements Runnable {
|
public class NotificationsService extends Service implements Runnable {
|
||||||
private static final String TAG = NotificationsService.class.getSimpleName();
|
private static final String TAG = NotificationsService.class.getSimpleName();
|
||||||
|
|
||||||
public static final String CHANNEL_ID = "IndependentPushServiceChannel";
|
public static final String SVC_CHANNEL_ID = "IndependentPushServiceChannel";
|
||||||
|
public static final String NOTIFS_CHANNEL_ID = "NotificationsPushServiceChannel";
|
||||||
private static final String INDEPENDENT_PUSH_NOTIFICATIONS_SERVICE = "independent-push-notifications-service";
|
private static final String INDEPENDENT_PUSH_NOTIFICATIONS_SERVICE = "independent-push-notifications-service";
|
||||||
private static final String WS_URL_PREF_KEY = "ws_url";
|
private static final String WS_URL_PREF_KEY = "ws_url";
|
||||||
|
|
||||||
|
private static final int NOTIFS_ID = 10;
|
||||||
|
|
||||||
private static final int CONNECT_TIMEOUT = 1000;
|
private static final int CONNECT_TIMEOUT = 1000;
|
||||||
private static final int RECONNECT_INTERVAL = 10000;
|
private static final int RECONNECT_INTERVAL = 10000;
|
||||||
private static final int PING_INTERVAL = 15000;
|
private static final int PING_INTERVAL = 15000;
|
||||||
@ -83,12 +90,12 @@ public class NotificationsService extends Service implements Runnable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
createNotificationChannel();
|
createServiceNotificationChannel();
|
||||||
|
|
||||||
Intent notificationIntent = new Intent(this, MainActivity.class);
|
Intent notificationIntent = new Intent(this, MainActivity.class);
|
||||||
PendingIntent pendingIntent = PendingIntent.getActivity(this,
|
PendingIntent pendingIntent = PendingIntent.getActivity(this,
|
||||||
0, notificationIntent, 0);
|
0, notificationIntent, 0);
|
||||||
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
|
Notification notification = new NotificationCompat.Builder(this, SVC_CHANNEL_ID)
|
||||||
.setContentTitle("Comunic")
|
.setContentTitle("Comunic")
|
||||||
.setContentText(getText(R.string.independent_push_notification_notification_text))
|
.setContentText(getText(R.string.independent_push_notification_notification_text))
|
||||||
.setSmallIcon(R.drawable.ic_notifications)
|
.setSmallIcon(R.drawable.ic_notifications)
|
||||||
@ -101,11 +108,23 @@ public class NotificationsService extends Service implements Runnable {
|
|||||||
return START_STICKY;
|
return START_STICKY;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createNotificationChannel() {
|
private void createServiceNotificationChannel() {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
NotificationChannel serviceChannel = new NotificationChannel(
|
NotificationChannel serviceChannel = new NotificationChannel(
|
||||||
CHANNEL_ID,
|
SVC_CHANNEL_ID,
|
||||||
"Foreground Service Channel",
|
"Foreground Service Channel",
|
||||||
|
NotificationManager.IMPORTANCE_LOW
|
||||||
|
);
|
||||||
|
NotificationManager manager = getSystemService(NotificationManager.class);
|
||||||
|
manager.createNotificationChannel(serviceChannel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createPushNotificationChannel() {
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
NotificationChannel serviceChannel = new NotificationChannel(
|
||||||
|
NOTIFS_CHANNEL_ID,
|
||||||
|
"Independent Push Service Channel",
|
||||||
NotificationManager.IMPORTANCE_DEFAULT
|
NotificationManager.IMPORTANCE_DEFAULT
|
||||||
);
|
);
|
||||||
NotificationManager manager = getSystemService(NotificationManager.class);
|
NotificationManager manager = getSystemService(NotificationManager.class);
|
||||||
@ -210,7 +229,56 @@ public class NotificationsService extends Service implements Runnable {
|
|||||||
if (!frame.isTextFrame())
|
if (!frame.isTextFrame())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Log.v(TAG, "Got text frame!");
|
Log.v(TAG, "Notification: " + frame.getPayloadText());
|
||||||
Log.v(TAG, frame.getPayloadText());
|
JSONObject jsonObject;
|
||||||
|
|
||||||
|
try {
|
||||||
|
jsonObject = new JSONObject(frame.getPayloadText());
|
||||||
|
|
||||||
|
|
||||||
|
if (jsonObject.has("drop_id"))
|
||||||
|
dropNotification(jsonObject.getString("drop_id"));
|
||||||
|
|
||||||
|
else if (jsonObject.has("id"))
|
||||||
|
sendNotification(new PushNotification(jsonObject));
|
||||||
|
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void dropNotification(String id) throws Exception {
|
||||||
|
new Handler(Looper.getMainLooper()).post(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
NotificationManagerCompat notifManager = NotificationManagerCompat.from(NotificationsService.this);
|
||||||
|
notifManager.cancel(id, NOTIFS_ID);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendNotification(PushNotification n) throws Exception {
|
||||||
|
new Handler(Looper.getMainLooper()).post(() -> postNotification(n));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void postNotification(PushNotification n) {
|
||||||
|
|
||||||
|
createPushNotificationChannel();
|
||||||
|
|
||||||
|
Intent intent = new Intent(this, MainActivity.class);
|
||||||
|
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
|
||||||
|
|
||||||
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFS_CHANNEL_ID)
|
||||||
|
.setSmallIcon(R.drawable.ic_notifications)
|
||||||
|
.setContentTitle(n.getTitle())
|
||||||
|
.setContentText(n.getBody())
|
||||||
|
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||||
|
.setContentIntent(pendingIntent)
|
||||||
|
.setAutoCancel(true);
|
||||||
|
|
||||||
|
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
|
||||||
|
notificationManagerCompat.notify(n.getId(), NOTIFS_ID, builder.build());
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,46 @@
|
|||||||
|
package org.communiquons.comunic.independentnotifications;
|
||||||
|
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
public class PushNotification {
|
||||||
|
private final String id;
|
||||||
|
private final int timeCreate;
|
||||||
|
private final int timeout;
|
||||||
|
private final String title;
|
||||||
|
private final String body;
|
||||||
|
private final String image;
|
||||||
|
|
||||||
|
public PushNotification(JSONObject input) throws JSONException {
|
||||||
|
id = input.getString("id");
|
||||||
|
timeCreate = input.getInt("time_create");
|
||||||
|
timeout = input.optInt("timeout");
|
||||||
|
title = input.optString("title");
|
||||||
|
body = input.optString("body");
|
||||||
|
image = input.optString("image");
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTimeCreate() {
|
||||||
|
return timeCreate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTimeout() {
|
||||||
|
return timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getBody() {
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getImage() {
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user