Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example for Message notifications
package com.creativtrendz.folio.notifications;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import java.io.IOException;
import java.util.ArrayList;
import org.jsoup.Jsoup;
import org.jsoup.select.Elements;
import com.creativetrends.folio.app.R;
import com.creativtrendz.folio.activities.MainActivity;
import com.creativtrendz.folio.activities.MyApplication;
import com.creativtrendz.folio.saxrssreader.RssItem;
import com.creativtrendz.folio.services.Connectivity;
import com.creativtrendz.folio.tray.TrayAppPreferences;
@SuppressWarnings("deprecation")
public class MessageNotifications extends Service {
private Handler handler = null;
private static Runnable runnable = null;
private String feedUrl;
private int timeInterval;
private SharedPreferences preferences;
private TrayAppPreferences trayPreferences;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
preferences = getSharedPreferences(getApplicationContext().getPackageName() + "_preferences", Context.MODE_MULTI_PROCESS);
trayPreferences = new TrayAppPreferences(getApplicationContext());
handler = new Handler();
runnable = new Runnable() {
public void run() {
Log.i("Folio Messenger", "isActivityVisible: " + Boolean.toString(trayPreferences.getBoolean("activity_visible", false)));
timeInterval = Integer.parseInt(preferences.getString("interval_pref", "12000"));
if (Connectivity.isConnected(getApplicationContext())) {
Log.i("Folio Messenger", "Data available. Starting Sync.");
new RssReaderTask().execute(feedUrl);
} else
Log.i("Folio Messenger", "Data unavailable. Stopping Sync.");
handler.postDelayed(runnable, timeInterval);
}
};
handler.postDelayed(runnable, 3000);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onDestroy() {
handler.removeCallbacksAndMessages(null);
super.onDestroy();
}
private class RssReaderTask extends AsyncTask<String, Void, ArrayList<RssItem>> {
private static final int MAX_RETRY = 3;
@Override
protected ArrayList<RssItem> doInBackground(String... params) {
@SuppressWarnings("unused")
String url;
ArrayList<RssItem> result = null;
int i = 0;
while (true) {
@SuppressWarnings("unused")
int tries = i + 1;
if (i >= MAX_RETRY || result != null) {
url = "https://m.facebook.com";
CookieSyncManager.createInstance(getApplicationContext());
CookieSyncManager.getInstance().sync();
}
url = "https://m.facebook.com";
CookieSyncManager.createInstance(getApplicationContext());
CookieSyncManager.getInstance().sync();
try {
Elements message = Jsoup.connect("http://m.facebook.com").cookie("https://m.facebook.com", CookieManager.getInstance().getCookie("https://m.facebook.com")).get().select("div#viewport").select("div#page").select("div._129-").select("#messages_jewel").select("span._59tg");
String amount = message.html();
if (Integer.parseInt(amount) == 1) {
messenger(getString(R.string.you_have) + amount + (getString(R.string.new_message)), amount, "http://m.facebook.com/messages");
} else if (Integer.parseInt(amount) > 1) {
messenger(getString(R.string.you_have) + amount + (getString(R.string.new_messages)), amount, "http://m.facebook.com/messages");
}
} catch (IOException e3) {
}
return result;
}
}
@Override
protected void onPostExecute(ArrayList<RssItem> result) {
final String savedDate = trayPreferences.getString("saved_date", "nothing");
try {
if (!result.get(1).getPubDate().toString().equals(savedDate))
if (!trayPreferences.getBoolean("activity_visible", false) || preferences.getBoolean("notifications_everywhere", true))
messenger(result.get(1).getTitle(), result.get(1).getDescription(), result.get(1).getLink());
preferences.edit();
trayPreferences.put("saved_date", result.get(1).getPubDate().toString());
Editor editor = MessageNotifications.this.preferences.edit();
editor.putString("lastdate", ((RssItem) result.get(1)).getPubDate().toString());
editor.apply();
} catch (NullPointerException ex) {
}
}
}
@SuppressLint("InlinedApi")
private void messenger(String title, String summary, String url) {
NotificationCompat.Builder fBuilder =
new NotificationCompat.Builder(this)
.setStyle(new NotificationCompat.BigTextStyle().bigText(title))
.setSmallIcon(R.drawable.ic_stat_f)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_folio_messenger))
.setContentTitle(getString(R.string.app_name))
.setContentText(title)
.setTicker(title)
.setWhen(System.currentTimeMillis())
.setOnlyAlertOnce(true)
.setCategory(Notification.CATEGORY_MESSAGE)
.setAutoCancel(true);
Intent newMessage = new Intent(this, MainActivity.class);
newMessage.putExtra("start_url", "https://m.facebook.com/messages");
Uri ringtoneUri = Uri.parse(preferences.getString("ringtone", "content://settings/system/notification_sound"));
fBuilder.setSound(ringtoneUri);
if (preferences.getBoolean("vibrate", false))
fBuilder.setVibrate(new long[] {500, 500});
if (preferences.getBoolean("led_light", false))
fBuilder.setLights(Color.BLUE, 1, 1);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
fBuilder.setPriority(Notification.PRIORITY_HIGH);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("start_url", url);
intent.setAction("MESSAGES_URL_ACTION");
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
fBuilder.setContentIntent(resultPendingIntent);
fBuilder.setOngoing(false);
Notification messageNote = fBuilder.build();
NotificationManager folioMessenger = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
folioMessenger.notify(1, messageNote);
}
public static void clearMessages() {
NotificationManager folioMessenger = (NotificationManager)
MyApplication.getContextOfApplication().getSystemService(Context.NOTIFICATION_SERVICE);
folioMessenger.cancel(1);
}
}
package com.creativtrendz.folio.notifications;
import com.creativtrendz.folio.activities.MyApplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
public class MessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
context = MyApplication.getContextOfApplication();
Intent messageIntent = new Intent(context, MessageNotifications.class);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (preferences.getBoolean("messages_activated", false))
context.startService(messageIntent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment