Search code examples
androiddrupaldrupal-7notifications

Android How receive Push notification by using drupal server


I follow all the steps as tutorial. tutorial android Push notification

Drupal Server side work link

My device is successfully register on drupal server by using GCM Id.

My problem is that , from server when sending push notification it shows 'Successfully send notification' , but this notification is not receive on phone.

Can any one please suggest me what should I do....


Solution

  • Finally I found my error.

    I am Trying to get notification message in Broadcast receiver, but it was wrong I m getting message in this service onMessage(Context context, Intent data).

    public class GCMIntentService extends GCMBaseIntentService{
    
    private static final String TAG = "GCMIntentService";
    
    public GCMIntentService() {
        super(SENDER_ID);
    }
    
    @Override
    protected void onError(Context arg0, String arg1) {
        // TODO Auto-generated method stub
    
    }
    
    @Override
    protected void onMessage(Context context, Intent data) {
    
        Log.i(TAG, "new message= ");
          String message = data.getExtras().getString("message");
          /*if (message.equals("") ) {
              //message="hellow senha , this is vc circle notification. You can check how it display long or short...";
              generateNotification(context, message);   
          }else{
              generateNotification(context, message);   
          }*/
          //message="hellow senha , this is vc circle notification. You can check how it display long or short...";
          generateNotification(context, message);   
    }
    
    private void generateNotification(Context context, String message) {
    
        int icon = R.drawable.vc_placeholder;
          long when = System.currentTimeMillis();
          NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    
          Intent notificationIntent = new Intent(context, SplashScreenActivity.class);
          notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
          PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0,
            notificationIntent, 0);
    
          NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                    context);
    
          Notification notification = mBuilder.setSmallIcon(icon).setTicker("VCCircle").setWhen(when)
                    .setContentTitle(context.getString(R.string.app_name))
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
                    .setContentIntent(resultPendingIntent)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.vc_placeholder))
                    .setContentText(message).build();
    
         notification.flags |= Notification.FLAG_AUTO_CANCEL;
          notificationManager.notify(0, notification);
    
          {
                // Wake Android Device when notification received
                PowerManager pm = (PowerManager) context
                        .getSystemService(Context.POWER_SERVICE);
                final PowerManager.WakeLock mWakelock = pm.newWakeLock(
                        PowerManager.FULL_WAKE_LOCK
                                | PowerManager.ACQUIRE_CAUSES_WAKEUP, "GCM_PUSH");
                mWakelock.acquire();
    
                // Timer before putting Android Device to sleep mode.
                Timer timer = new Timer();
                TimerTask task = new TimerTask() {
                    public void run() {
                        mWakelock.release();
                    }
                };
                timer.schedule(task, 5000);
            }
    
    
    
    }
    
    @Override
    protected void onRegistered(Context context, String registrationId) {
        Log.i(TAG, "Device registered: regId = " + registrationId);
        displayMessage(context, getString(R.string.gcm_registered));
        boolean te=ServerUtilities.register(context, registrationId);
        System.out.println("Device Register : "+te);
    }
    
    @Override
    protected void onUnregistered(Context arg0, String arg1) {
        // TODO Auto-generated method stub
    
    }
    

    }