package survivingwithandroid.com.parsepushtutorial;
/*
* Copyright (C) 2015, francesco Azzola
*
*(http://www.survivingwithandroid.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* 03/09/15
*/
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.parse.ParsePushBroadcastReceiver;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;
public class JSONCustomReceiver extends ParsePushBroadcastReceiver {
@Override
protected void onPushReceive(Context context, Intent intent) {
super.onPushReceive(context, intent);
Log.d("Push", "Push received");
if (intent == null)
return ;
String jsonData = intent.getExtras().getString("com.parse.Data");
Log.d("Push", "JSON Data ["+jsonData+"]");
String data = getData(jsonData);
// Add custom intent
Intent cIntent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, cIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Create custom notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_not_alert)
.setContentText(data)
.setContentTitle("Notification from Parse")
.setContentIntent(pendingIntent);
Notification notification = builder.build();
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(1410, notification);
}
private String getData(String jsonData) {
// Parse JSON Data
try {
System.out.println("JSON Data ["+jsonData+"]");
JSONObject obj = new JSONObject(jsonData);
return obj.getString("message");
}
catch(JSONException jse) {
jse.printStackTrace();
}
return "";
}
}