Android Open Source - as3c2dm C2 D M Broadcast Receiver






From Project

Back to project page as3c2dm.

License

The source code is released under:

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUC...

If you think the Android project as3c2dm listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

//////////////////////////////////////////////////////////////////////////////////////
///*ww w .j  a  v a2  s  .c  o m*/
//  Copyright 2011 Piotr Walczyszyn (http://riaspace.com | @pwalczyszyn)
//  
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//  
//    http://www.apache.org/licenses/LICENSE-2.0
//  
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//  
//////////////////////////////////////////////////////////////////////////////////////

package com.riaspace.c2dm;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;

import com.adobe.fre.FREContext;

public class C2DMBroadcastReceiver extends BroadcastReceiver {

  public C2DMBroadcastReceiver() {
  }

  @Override
  public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(
        "com.google.android.c2dm.intent.REGISTRATION")) {
      handleRegistration(context, intent);
    } else if (intent.getAction().equals(
        "com.google.android.c2dm.intent.RECEIVE")) {
      handleMessage(context, intent);
    }
  }

  private void handleRegistration(Context context, Intent intent) {
    FREContext freContext = C2DMExtension.context;
    String registration = intent.getStringExtra("registration_id");

    if (intent.getStringExtra("error") != null) {
      String error = intent.getStringExtra("error");
      Log.d("as3c2dm", "Registration failed with error: " + error);
      if (freContext != null) {
        freContext.dispatchStatusEventAsync(error, "error");
      }
    } else if (intent.getStringExtra("unregistered") != null) {
      Log.d("as3c2dm", "Unregistered successfully");
      if (freContext != null) {
        freContext.dispatchStatusEventAsync("unregistered",
            "unregistered");
      }
    } else if (registration != null) {
      Log.d("as3c2dm", "Registered successfully");
      if (freContext != null) {
        freContext.dispatchStatusEventAsync(registration, "registered");
      }
    }
  }

  private void handleMessage(Context context, Intent intent) {
    try {
      NotificationManager nm = (NotificationManager) context
          .getSystemService(Context.NOTIFICATION_SERVICE);

      int icon = R.drawable.icon;
      long when = System.currentTimeMillis();

      String parameters = intent.getStringExtra("parameters");
      CharSequence tickerText = intent.getStringExtra("tickerText");
      CharSequence contentTitle = intent.getStringExtra("contentTitle");
      CharSequence contentText = intent.getStringExtra("contentText");

      Intent notificationIntent = new Intent(context, 
          Class.forName(context.getPackageName() + ".AppEntry"));
      notificationIntent.setData(Uri.parse(parameters));

      PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
          notificationIntent, 0);

      Notification notification = new Notification(icon, tickerText, when);
      notification.flags |= Notification.FLAG_AUTO_CANCEL;
      notification.setLatestEventInfo(context, contentTitle, contentText,
          contentIntent);

      nm.notify(1, notification);
    } catch (Exception e) {
      Log.e("as3c2dm", "Error activating application:", e);
    }
  }
}




Java Source Code List

com.riaspace.c2dm.C2DMBroadcastReceiver.java
com.riaspace.c2dm.C2DMExtensionContext.java
com.riaspace.c2dm.C2DMExtension.java
com.riaspace.c2dm.C2DMRegisterFunction.java
com.riaspace.c2dm.C2DMUnregisterFunction.java