1. Use a service to receive gcm
public class MyGcmListenerService extends GcmListenerService { private static final String TAG = "MyGcmListenerService"; /** * Called when message is received. * * @param from SenderID of the sender. * @param data Data bundle containing message data as key/value pairs. * For Set of keys use data.keySet(). */ // [START receive_message] @Override public void onMessageReceived(String from, Bundle data) { String message = data.getString("message"); Log.d(TAG, "From: " + from); Log.d(TAG, "Message: " + message); for (String key : data.keySet() ) { Log.d(TAG, "key : " + key); } String messageId = data.getString("google.message_id"); String messageKey = data.getString("message"); String collapse_key = data.getString("collapse_key"); String score = data.getString("score"); String payload = data.getString("data.payload"); String extraMap = data.getString("data.extraMap"); Log.d(TAG, "Message id : " + messageId); Log.d(TAG, "message_key : " + messageKey + ",collapse_key : " + collapse_key + ",payload : " + payload + ",extraMap : " + extraMap); if (message == null || message.equals("")) { Log.d(TAG, "Received message from GCM not success..."); } else { Log.d(TAG, "success received from GCM : " + message); } if (from.startsWith("/topics/")) { // message received from some topic. Log.d(TAG, "message received from some topic."); } else { // normal downstream message. Log.d(TAG, "normal downstream message."); } // [START_EXCLUDE] /** * Production applications would usually process the message here. * Eg: - Syncing with server. * - Store message in local database. * - Update UI. */ /** * In some cases it may be useful to show a notification indicating to the user * that a message was received. */ sendNotification(message,score); // [END_EXCLUDE] } 。。。。。。
2. Create push (two functional parts: 1. Click Notification to jump to AlertDialog 2. Pop up the box in MainActivity (using LocalBroadcast)
private void sendNotification(String title,String content) { Intent intent = new Intent(this, AlertActivity.class); Bundle bundle = new Bundle(); bundle.putString("title",title); bundle.putString("content",content); intent.putExtra("alert_gcm", bundle); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //PendingIntent is the Activity opened after clicking the push, the third parameter Intent can set the jump //Here is to jump to a custom AlertActivity, which opens the AlertDialog (you can set the Activity background color to transparent) PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_stat_ic_notification) .setContentTitle(title) .setContentText(content) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); //MainActivity to received gcm notification Intent gcmComplete = new Intent(QuickstartPreferences.GCM_COMPLETE); Log.d(TAG, "title : " + title + " content : " + content); gcmComplete.putExtra("gcmBundle", bundle); LocalBroadcastManager.getInstance (this) .sendBroadcast (gcmComplete); }
If clicking on Notification fails to pass the value, generally adjust the value of a flag:
flags has four values:
int FLAG_CANCEL_CURRENT: If this PendingIntent already exists, cancel the current one before generating a new one.
int FLAG_NO_CREATE: If the PendingIntent does not exist, return null instead of creating a PendingIntent.
int FLAG_ONE_SHOT: The PendingIntent can only be used once, and is automatically canceled after the send() method is executed.
int FLAG_UPDATE_CURRENT: If the PendingIntent already exists, update the current data with the new incoming Intent.
We need to change the last parameter to PendingIntent.FLAG_UPDATE_CURRENT, so that in the started Activity, we can use the method of receiving Intent to transmit data normally.
3.MainActivity establishes the receiver:
mGcmNotificationBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getBundleExtra("gcmBundle"); String title = bundle.getString("title"); String content = bundle.getString("content"); Log.d(TAG, "title : " + title + " content : " + content); //Dialog to show content new AlertDialog.Builder(MainActivity.this,R.style.dialogTheme) .setTitle(title) .setMessage(content) .setNegativeButton("OK", null) .show(); } };
Register the receiver:
private void registerReceiver(){ if (!isReceiverGcm){ LocalBroadcastManager.getInstance(this).registerReceiver(mGcmNotificationBroadcastReceiver, new IntentFilter(QuickstartPreferences.GCM_COMPLETE)); isReceiverGcm = true; } }
@Override protected void onResume() { super.onResume(); registerReceiver(); } @Override protected void onPause() { LocalBroadcastManager.getInstance(this).unregisterReceiver(mGcmNotificationBroadcastReceiver); isReceiverGcm = false; super.onPause(); }
4.AlertActivity:
Intent intent = getIntent(); Bundle bundle = intent.getBundleExtra("alert_gcm"); String title = bundle.getString("title"); String content = bundle.getString("content"); Log.d(TAG, "title : " + title + " content : " + content); //Dialog to show content new AlertDialog.Builder(this,R.style.dialogTheme) .setTitle(title) .setMessage(content) .setNegativeButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent i = new Intent(AlertActivity.this, MainActivity.class); startActivity (i); } }) .show();