package com.geel9.facepunch; import java.io.StringReader; import java.io.StringWriter; import java.net.URL; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.util.Config; import com.geel9.facepunch.R; import com.geel9.facepunch.activities.DevMessageActivity; import com.geel9.facepunch.activities.ThreadActivity; public class C2DMReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) { handleRegistration(context, intent); } else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) { handleMessage(context, intent); } } private void handleRegistration(Context context, Intent intent) { SharedPreferences p = context.getSharedPreferences("com.geel9.facepunch_preferences", Context.MODE_PRIVATE); int userId = p.getInt("userid", -1); System.out.println("Registering 2. Id: " + userId); if(userId == -1) return; String registration = intent.getStringExtra("registration_id"); if (intent.getStringExtra("error") != null) { } else if (intent.getStringExtra("unregistered") != null) { } else if (registration != null) { final String endpoint = "http://www.facepunch.walkthedinosaur.net/registerphone.php"; String postData = "userid=" + userId + "&phoneid=" + registration; final StringWriter sw = new StringWriter(); final StringReader reader = new StringReader(postData); new Thread(new Runnable(){ public void run() { try { HTTPRequestPoster.postData(reader, new URL(endpoint), sw); String output = sw.toString(); String test = output + "1"; }catch (Exception e) { } } }).start(); } } protected void handleMessage(Context context, Intent intent) { SharedPreferences p = context.getSharedPreferences("com.geel9.facepunch_preferences", Context.MODE_PRIVATE); int userId = p.getInt("userid", -1); boolean allow = p.getBoolean("prefReceiveMessages", true); if(!allow || userId == -1) return; String type = intent.getExtras().getString("type"); if(type.equals("message")){ String message = intent.getExtras().getString("message"); int id = Integer.parseInt(intent.getExtras().getString("NID")); showMessageNotification(message, id, context); } else if(type.equals("subupdate")){ String message = intent.getExtras().getString("subscription"); int threadID = Integer.parseInt(intent.getExtras().getString("threadID")); String poster = intent.getExtras().getString("poster"); String threadName = intent.getExtras().getString("threadName"); showNewPostNotification(poster, threadID, threadName, context); } } public String removeHTMLTags(String toRemoveFrom){ return toRemoveFrom.replace("<br/>", "\n").replace("<b>", "").replace("</b>", ""); } public void showMessageNotification(String message, int notificationID, Context context){ String notificationMessage = removeHTMLTags(message); String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns); int icon = R.drawable.icon; CharSequence tickerText = notificationMessage; long when = System.currentTimeMillis(); Notification notification = new Notification(icon, tickerText, when); notification.defaults |= Notification.DEFAULT_SOUND; CharSequence contentTitle = "Message from developer"; Intent threadIntent = new Intent(context, DevMessageActivity.class); threadIntent.putExtra("message", message); threadIntent.putExtra("id", notificationID); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, threadIntent, Intent.FLAG_ACTIVITY_NEW_TASK ); notification.setLatestEventInfo(context, contentTitle, notificationMessage, contentIntent); mNotificationManager.notify(notificationID, notification); } public void showNewPostNotification(String poster, int threadID, String threadTitle, Context arg0){ String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) arg0.getSystemService(ns); int icon = R.drawable.icon; String message = "New post in " + threadTitle + " by " + poster; CharSequence tickerText = message; long when = System.currentTimeMillis(); Notification notification = new Notification(icon, tickerText, when); notification.defaults |= Notification.DEFAULT_SOUND; CharSequence contentTitle = "New Post"; Intent threadIntent = new Intent(arg0, ThreadActivity.class); threadIntent.putExtra("threadName", threadTitle); threadIntent.putExtra("threadId", threadID); threadIntent.putExtra("threadPage", -1); PendingIntent contentIntent = PendingIntent.getActivity(arg0, 0, threadIntent, Intent.FLAG_ACTIVITY_NEW_TASK ); notification.setLatestEventInfo(arg0, contentTitle, message, contentIntent); mNotificationManager.notify(threadID, notification); } }