2

My application is described as follows: the application will receive push information from the server and displayed on the notification bar of android. When I click on the notification of the application will load the url I get from the server. But my problem now, I can not send the url received from the server to the original activity.

Below is the error I get when I run the application.

04-09 11:25:26.503: E/AndroidRuntime(1030): FATAL EXCEPTION:  IntentService[GCMIntentService-810012644757-1]
04-09 11:25:26.503: E/AndroidRuntime(1030): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
04-09 11:25:26.503: E/AndroidRuntime(1030):     at android.app.ContextImpl.startActivity(ContextImpl.java:624)
04-09 11:25:26.503: E/AndroidRuntime(1030):     at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)
04-09 11:25:26.503: E/AndroidRuntime(1030):     at com.ketan.demo.GCMIntentService.generateNotification(GCMIntentService.java:118)
04-09 11:25:26.503: E/AndroidRuntime(1030):     at com.ketan.demo.GCMIntentService.onMessage(GCMIntentService.java:70)
04-09 11:25:26.503: E/AndroidRuntime(1030):     at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:179)
04-09 11:25:26.503: E/AndroidRuntime(1030):     at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
04-09 11:25:26.503: E/AndroidRuntime(1030):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-09 11:25:26.503: E/AndroidRuntime(1030):     at android.os.Looper.loop(Looper.java:130)
04-09 11:25:26.503: E/AndroidRuntime(1030):     at android.os.HandlerThread.run(HandlerThread.java:60)

My code:

private static void generateNotification(Context context, String message) {
    int icon = R.drawable.ic_stat_gcm;
    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().setClass(context.getApplicationContext(), DemoActivity.class);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);                     
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent =
            PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);
    notificationIntent.putExtra("URL", message);
    // Launch the new activity and add the additional flags to the intent
    context.getApplicationContext().startActivity(notificationIntent);

    /*Intent i = new Intent().setClass(context.getApplicationContext(), DemoActivity.class);  
    i.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);                     
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
    i.putExtra("URL", message);
    // Launch the new activity and add the additional flags to the intent
    context.getApplicationContext().startActivity(i);*/
}

Activity Demo

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    checkNotNull(SERVER_URL, "http://10.0.2.2:8080/gcm-demo-server/");
   // checkNotNull(SENDER_ID, "972801588344");
    // Make sure the device has the proper dependencies.
    GCMRegistrar.checkDevice(this);
    // Make sure the manifest was properly set - comment out this line
    // while developing the app, then uncomment it when it's ready.
    GCMRegistrar.checkManifest(this);
    setContentView(R.layout.main);
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        url = extras.getString("URL");
    }
    web=(WebView) findViewById(R.id.webView1);
    if(url==null)
    {    
        web.loadUrl("http://kqxs.vn/trang_chu");
    }else
    {
        web.loadUrl(url);
    }
}

I fixed the following by

 private static void generateNotification(Context context, String message) {
    int icon = R.drawable.ic_stat_gcm;
    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, DemoActivity.class);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
    notificationIntent.putExtra("URL", message);
    PendingIntent intent =
            PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);
}

But I have another problem, which is the only application I load the correct url for the first time, from the 2nd, after receiving a new url from the server, but my application loaded url of the first time. I have tried and tried many times but still getting results so...

1 Answers1

0

please see following question:

Android:What is difference between setFlags and addFlags for intent

Intent.setFlags : means you are replacing the old flags by adding new flags to intent

Intent.addFlags : means you are appending new flags with already added to intent

so, you will need to use notificationIntent.addFlags instead of notificationIntent.setFlags for appending Intent.FLAG_ACTIVITY_NEW_TASK flag to notificationIntent intent as:

.......
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);                  
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
        Intent.FLAG_ACTIVITY_SINGLE_TOP);
.......
Community
  • 1
  • 1
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213