Skip to content

Commit e9734e9

Browse files
committed
Reworked notification to utils
Reworked notification to utils
1 parent 8057334 commit e9734e9

File tree

2 files changed

+54
-18
lines changed

2 files changed

+54
-18
lines changed

app/src/main/java/nl/hnogames/domoticz/Service/GeofenceTransitionsIntentService.java

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import nl.hnogames.domoticz.Interfaces.setCommandReceiver;
4949
import nl.hnogames.domoticz.MainActivity;
5050
import nl.hnogames.domoticz.R;
51+
import nl.hnogames.domoticz.Utils.NotificationUtil;
5152
import nl.hnogames.domoticz.Utils.SharedPrefUtil;
5253
import nl.hnogames.domoticz.Utils.UsefulBits;
5354

@@ -91,9 +92,8 @@ protected void onHandleIntent(Intent intent) {
9192
for (Geofence geofence : geoFenceEvent.getTriggeringGeofences()) {
9293
LocationInfo locationFound = mSharedPrefs.getLocation(Integer.valueOf(geofence.getRequestId()));
9394
Log.d(TAG, "Triggered geofence location: " + locationFound.getName());
94-
9595
if (mSharedPrefs.isGeofenceNotificationsEnabled())
96-
sendNotification("Entering " + locationFound.getName(), "Entering one of the locations");
96+
NotificationUtil.sendSimpleNotification("Entering " + locationFound.getName(), "Entering one of the locations", this);
9797

9898
if (locationFound.getSwitchidx() > 0) {
9999
handleSwitch(locationFound.getSwitchidx(), true);
@@ -103,9 +103,8 @@ protected void onHandleIntent(Intent intent) {
103103
for (Geofence geofence : geoFenceEvent.getTriggeringGeofences()) {
104104
LocationInfo locationFound = mSharedPrefs.getLocation(Integer.valueOf(geofence.getRequestId()));
105105
Log.d(TAG, "Triggered geofence location: " + locationFound.getName());
106-
107106
if (mSharedPrefs.isGeofenceNotificationsEnabled())
108-
sendNotification("Leaving " + locationFound.getName(), "Leaving one of the locations");
107+
NotificationUtil.sendSimpleNotification("Leaving " + locationFound.getName(), "Leaving one of the locations", this);
109108

110109
if (locationFound.getSwitchidx() > 0) {
111110
handleSwitch(locationFound.getSwitchidx(), false);
@@ -192,20 +191,6 @@ private void onErrorHandling(Exception error)
192191
}
193192
}
194193

195-
private void sendNotification(String title, String text) {
196-
NotificationCompat.Builder builder =
197-
new NotificationCompat.Builder(this)
198-
.setSmallIcon(R.drawable.ic_launcher)
199-
.setContentTitle(title)
200-
.setContentText(text);
201-
int NOTIFICATION_ID = 12345;
202-
203-
Intent targetIntent = new Intent(this, MainActivity.class);
204-
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
205-
builder.setContentIntent(contentIntent);
206-
NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
207-
nManager.notify(NOTIFICATION_ID, builder.build());
208-
}
209194

210195

211196
@Override
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (C) 2015 Domoticz
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*
21+
*/
22+
23+
package nl.hnogames.domoticz.Utils;
24+
25+
import android.app.NotificationManager;
26+
import android.app.PendingIntent;
27+
import android.content.Context;
28+
import android.content.Intent;
29+
import android.support.v4.app.NotificationCompat;
30+
31+
import nl.hnogames.domoticz.MainActivity;
32+
import nl.hnogames.domoticz.R;
33+
34+
public class NotificationUtil {
35+
36+
public static void sendSimpleNotification(String title, String text, Context context) {
37+
NotificationCompat.Builder builder =
38+
new NotificationCompat.Builder(context)
39+
.setSmallIcon(R.drawable.ic_launcher)
40+
.setContentTitle(title)
41+
.setContentText(text);
42+
int NOTIFICATION_ID = 12345;
43+
44+
Intent targetIntent = new Intent(context, MainActivity.class);
45+
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
46+
builder.setContentIntent(contentIntent);
47+
NotificationManager nManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
48+
nManager.notify(NOTIFICATION_ID, builder.build());
49+
}
50+
51+
}

0 commit comments

Comments
 (0)