Search code examples
androidpush-notificationurbanairship.com

Open android app on clicking on the push notification with urban airship?


I am sending the push notifications with the help of Urban Airship, and getting the notifications successfully also. But when I am clicking on the notification, it is not opening my app. So what can I do for opening my app ?

and getting the following error in logcate:-

02-10 18:53:44.137: W/xxx - UALib(6840): Activity com.aaa.yyy.SplashActivity@40dcb458 was not manually added during onStart(). Call UAirship.shared().getAnalytics().activityStarted in every activity's onStart() method.

Solution

  • Yes after doing the Google I got the answer:- We need to create an IntentReceiver.java class as follows:-

     public class IntentReceiver extends BroadcastReceiver{
     private static final String logTag = "PushSample";
     @Override
     public void onReceive(Context context, Intent intent) {
        Log.i(logTag, "Received intent: " + intent.toString());
        String action = intent.getAction();
    
         if (action.equals(PushManager.ACTION_PUSH_RECEIVED)) {
             int id = intent.getIntExtra(PushManager.EXTRA_NOTIFICATION_ID, 0);
             Log.i(logTag, "Received push notification. Alert: "
                        + intent.getStringExtra(PushManager.EXTRA_ALERT)
                        + " [NotificationID="+id+"]");
    
                logPushExtras(intent);
         }else if (action.equals(PushManager.ACTION_NOTIFICATION_OPENED)) {
              Log.i(logTag, "User clicked notification. Message: " + intent.getStringExtra(PushManager.EXTRA_ALERT));
    
              logPushExtras(intent);
    
              Intent launch = new Intent(Intent.ACTION_MAIN);
              launch.setClass(UAirship.shared().getApplicationContext(), MainActivity.class);
              launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              UAirship.shared().getApplicationContext().startActivity(launch);
    
        }else if (action.equals(PushManager.ACTION_REGISTRATION_FINISHED)) {
            Log.i(logTag, "Registration complete. APID:" + intent.getStringExtra(PushManager.EXTRA_APID)
                    + ". Valid: " + intent.getBooleanExtra(PushManager.EXTRA_REGISTRATION_VALID, false));
        }
    }
    
    private void logPushExtras(Intent intent) {
        Set<String> keys = intent .getExtras().keySet();
        for(String key: keys){
            List<String> ignoredKeys = (List<String>)Arrays.asList("collapse_key", "from", PushManager.EXTRA_NOTIFICATION_ID,PushManager.EXTRA_PUSH_ID, PushManager.EXTRA_ALERT);
            if(ignoredKeys.contains(key)){
                continue;
            }
             Log.i(logTag, "Push Notification Extra: ["+key+" : " + intent.getStringExtra(key) + "]");
        }   }   
      }
    

    After that we need to call the following method in PushNotification.java. Here is the code.

    public class PushNotification extends Application{
    
        @Override
        public void onCreate() {
    
            AirshipConfigOptions options = AirshipConfigOptions.loadDefaultOptions(this);
            options.developmentAppKey = "xxx";
            options.developmentAppSecret = "yyy";
            options.productionAppKey = "zzz";
            options.inProduction= false;
    
            UAirship.takeOff(this, options);
            PushManager.enablePush();
    
            String apid = PushManager.shared().getAPID();
            Logger.info("My Application onCreate - App APID: " + apid);
    
            PushManager.shared().setIntentReceiver(IntentReceiver.class);
    
    
        }
    }