如何使用新的GCM API将GCM推送通知消息存储到sqlite数据库中?

时间:2015-04-14 08:29:27

标签: android google-cloud-messaging

我是Android和编程的新手,并且正在开发GCM新API。一切正常,我已成功实施GCM。但我需要将消息保存到数据库表中。但是当我实现下面的数据库操作代码时,GCM和数据库一样停止工作。

 public class GCMNotificationIntentService extends IntentService {

public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder; 
DBHelper db;

public GCMNotificationIntentService() {
    super("GcmIntentService");
//   db= new DBHelper(this.getBaseContext());
}

public static final String TAG = "GCMNotificationIntentService";

@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

    String messageType = gcm.getMessageType(intent);

    if (!extras.isEmpty()) {
        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
                .equals(messageType)) {
            sendNotification("Send error: " + extras.toString());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
                .equals(messageType)) {
            sendNotification("Deleted messages on server: "
                    + extras.toString());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
                .equals(messageType)) {

            for (int i = 0; i < 3; i++) {
                Log.i(TAG,
                        "Working... " + (i + 1) + "/5 @ "
                                + SystemClock.elapsedRealtime());
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                }

            }
            Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());

            String eid =  extras.get(Config.EVENT_ID).toString();
            String loc =  extras.get(Config.LOCATION).toString();
            String mag =  extras.get(Config.MAGNITUDE).toString();
            String lat =  extras.get(Config.LATITUDE).toString();
            String lon =  extras.get(Config.LONGITUDE).toString();
            String time = extras.get(Config.ORIGINE_TIME).toString();


                db.insertEventInfo(eid,loc,mag,lat,lon,time);

            sendNotification("["+mag+"]"+" "+loc+" "+time);

            Log.i(TAG, "Received: " + extras.toString());
        }
    }
    GcmBroadcastReceiver.completeWakefulIntent(intent);
}

private void sendNotification(String msg) {
    Log.d(TAG, "Preparing to send notification...: " + msg);
    mNotificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, EarthquakeListActivity.class), 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.gcm_cloud)
            .setContentTitle("ITEWC Notification")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    Log.d(TAG, "Notification sent successfully.");
  }

}

DBHelper Class是

public class DBHelper extends SQLiteOpenHelper{

public SQLiteDatabase db;
public String DBPath;   
public static final int version = 1;
public static Context currentContext;

public static  String DATABASE_NAME = "EarthquakeList";
public static  String EARTHQUAKE_TABLE_NAME="earthquakes";
public static  String EARTHQUAKE_ID = "eventId";
public static  String EARTHQUAKE_LOCATION = "location";
public static  String EARTHQUAKE_ORIGIN_TIME = "originTime";
public static  String EARTHQUAKE_LATITUDE = "latitude";
public static  String EARTHQUAKE_LONGITUDE = "longitude";
public static  String EARTHQUAKE_MAGNITUDE = "magnitude";   

static final String TAG = "DBHelper";

public DBHelper(Context context) {
super(context, DATABASE_NAME, null, version);
}

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL(
              "CREATE TABLE IF NOT EXISTS earthquakes  (eventId  VARCHAR ,location VARCHAR , originTime VARCHAR , latitude VARCHAR ,longitude VARCHAR , magnitude VARCHAR)"
              );

    Log.d("DBHelper", "Database Created ");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
     db.execSQL("DROP TABLE IF EXISTS earthquakes");
      onCreate(db);

}

 public void insertEventInfo  (String eventId, String location, String originTime, String latitude,String longitude, String magnitude){


      SQLiteDatabase db = this.getWritableDatabase();         

      ContentValues values = new ContentValues();
      values.put(EARTHQUAKE_ID, eventId);
      values.put(EARTHQUAKE_LOCATION, location);
      values.put(EARTHQUAKE_ORIGIN_TIME, originTime);
      values.put(EARTHQUAKE_LATITUDE, latitude);
      values.put(EARTHQUAKE_LONGITUDE,longitude );
      values.put(EARTHQUAKE_MAGNITUDE, magnitude);

      db.insert(EARTHQUAKE_TABLE_NAME, null, values);


      Log.d("DBHelper", "in Insert Method ");
      db.close();        
    }   
}

1 个答案:

答案 0 :(得分:0)

您必须初始化DBHelper类。

db= new DBHelper(this);

我建议您必须使用库进行数据库处理,如糖ORM这里是链接 http://satyan.github.io/sugar/