Android - Receive Push notification and display it - not quite understanding

15,175

Solution 1

I figured it out. I finally found a pretty good working project for which you can study the code and understand it. It took me days of googling to find something that wasn't just a piece here and a piece there but instead a whole project. I figured I'd be nice enough to post it in case someone else could use it.

https://github.com/Guti/Google-Cloud-Messaging--Titanium-

Solution 2

Use Below code it's properly work:

@Override
public void onDeletedMessages() {

    sendNotification("Message Deleted On server");
    super.onDeletedMessages();
}

@Override
public void onMessageReceived(String from, Bundle data) {

    sendNotification("Received: " + data.getString("message"));
    super.onMessageReceived(from, data);
}

@Override
public void onMessageSent(String msgId) {

    sendNotification("Message Sent: " + msgId);
    super.onMessageSent(msgId);
}

@Override
public void onSendError(String msgId, String error) {

    sendNotification("Message Sent Error: " + msgId + "Error: " + error);
    super.onSendError(msgId, error);
}

private void sendNotification(String msg) {

    Intent intent = new Intent(this, MessageView.class);
    intent.putExtra("Message", msg);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            intent, PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager
            .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.logo_y)
            .setContentTitle("CCD Message").setContentText(msg)
            .setAutoCancel(true).setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());

}
Share:
15,175
Panama Jack
Author by

Panama Jack

Web enthusiast that enjoys all aspects of the internet and programming. I'm a fan of open source. Mostly my personal setups include Linux (RHEL/CentOS), Apache 2.4, PostgreSQL 9.X, RabbitMQ, Redis cluster Most of my web development is using PHP, jQuery, HTML5, CSS, some python. My servers include many cloud services working as Web, Email, DB, Redis Caching cluster, DNS, RabbitMQ, Media encoding(FFMPEG). Anyway, I help out here when I can.

Updated on June 04, 2022

Comments

  • Panama Jack
    Panama Jack almost 2 years

    Ok so I finally got setup to register devices for push notifications. I found this code to receive new notifications and display it. Thing is I'm not sure where it goes. I'm pretty new to Android programming so any help is appreciated. I have a service class called GCMService below.

    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;
    
    import com.google.android.gcm.GCMBaseIntentService;
    
    public class GCMService extends GCMBaseIntentService {
    
        private static final String TAG = "GCMService";
    
        public GCMService() {
            super();
        }
    
        @Override
        protected void onRegistered(Context context, String regId) {
            Log.i(TAG, "Device registered: regId= " + regId);
        }
    
        @Override
        protected void onUnregistered(Context context, String regId) {
            Log.i(TAG, "Device unregistered");
        }
    
        @Override
        protected void onMessage(Context context, Intent intent) {
            Log.i(TAG, "Received message");
        }
    
        @Override
        public void onError(Context context, String errorId) {
            Log.i(TAG, "Received error: " + errorId);
        }
    
        @Override
        protected boolean onRecoverableError(Context context, String errorId) {
            Log.i(TAG, "Received recoverable error: " + errorId);
            return super.onRecoverableError(context, errorId);
        }
    }
    

    Where does the function below go in relation to my class above to receive a new message?

    private static void generateNotification(Context context, String message) {
    
        int icon = R.drawable.ic_launcher;
        long when = System.currentTimeMillis();
    
        NotificationManager notificationManager = (NotificationManager) 
                context.getSystemService(Context.NOTIFICATION_SERVICE);
    
        Notification notification = new Notification(icon, message, when);      
    
        String title = context.getString(R.string.app_name);
        Intent notificationIntent = new Intent(context, LauncherActivity.class);
    
          PendingIntent pintent = PendingIntent.getActivity(context, 0, intent,
     Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE; 
        notificationManager.notify(1, notification);
    }