当在后台收到GCM通知时,当用户单击该通知时,它将链接到我的应用程序,然后我可以在此GCM通知中获取数据包。
但是有没有办法在不单击通知的情况下获取通知中的数据? 比如,如果通知来了,用户不点击通知,而是点击应用,直接打开应用。此时,是否可以在用户打开应用程序时获取所有通知有效负载?
当您收到 GCM 消息时,Android 会调用您的 BroadcastReceiver,即使您的应用在后台运行:
public class GCMBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
String action = intent.getAction();
L.m("onReceive GCM called!");
//Get your data
String id = intent.getStringExtra("id");
String tickerText = intent.getStringExtra("tickerText");
String title = intent.getStringExtra("title");
String msg = intent.getStringExtra("message");
String dateTime = intent.getStringExtra("datetime");
//Here, You can save your data in SQLite
//For example: I need to save title, msg and dateTime
DBGcmMessages dbGcmMessages = new DBGcmMessages(context);
dbGcmMessages.insert(title, msg, dateTime);
//Then, you can show notification or show nothing!!!
}
}
这是DBGcmMessages
内容:
public class DBGcmMessages extends SQLiteOpenHelper {
private Context mContext;
private SQLiteDatabase mDatabase;
private static final String DB_NAME = "gcm_message_db";
private static final int DB_VERSION = 1;
private static final String TABLE = "login";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_TITLE = "_title";
private static final String COLUMN_TEXT = "_text";
private static final String COLUMN_DATE_TIME = "_date_time";
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
COLUMN_TITLE + " nvarchar(255)," +
COLUMN_TEXT + " text," +
COLUMN_DATE_TIME + " nvarchar(255));";
public DBGcmMessages(Context context) {
super(context, DB_NAME, null, DB_VERSION);
mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_TABLE);
L.m("Create table gcm messages successfully!");
} catch (SQLiteException exception) {
L.m(exception.getMessage());
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
L.m("upgrade table gcm message executed!");
db.execSQL("DROP TABLE " + TABLE + " IF EXISTS;");
onCreate(db);
}
catch (SQLiteException exception) {
L.m(exception.getMessage());
}
}
public void insert(String title, String text, String dateTime) {
try {
if (title == null || text == null) return;
this.mDatabase = this.getWritableDatabase();
//Insert
String sql = "INSERT INTO " + TABLE + "(" +
COLUMN_TITLE + "," +
COLUMN_TEXT + "," +
COLUMN_DATE_TIME + ") VALUES (?,?,?);";
SQLiteStatement statement = this.mDatabase.compileStatement(sql);
statement.bindString(1, title);
statement.bindString(2, text);
statement.bindString(3, dateTime);
statement.execute();
L.m("Insert gcm message successfully!");
}
finally {
if(this.mDatabase != null) this.mDatabase.close();
this.close();
}
}
public void delete(int id) {
try {
this.mDatabase = this.getWritableDatabase();
this.mDatabase.execSQL("Delete from " + TABLE + " where " + COLUMN_ID + "='" + id + "'");
L.m("Delete gcm message successfully!");
}
finally {
if(this.mDatabase != null) this.mDatabase.close();
this.close();
}
}
public ArrayList<GCMMessage> getAll() {
this.mDatabase = this.getWritableDatabase();
Cursor result = null;
try {
result = this.mDatabase.rawQuery("select * from " + TABLE + " order by " + COLUMN_ID + " desc", null);
int rows = result.getCount();
if (rows == 0) return null;
ArrayList<GCMMessage> messages = new ArrayList<>();
result.moveToFirst();
for (int i = 0; i < rows; i++) {
int id = result.getInt(0);
String title = result.getString(1);
String text = result.getString(2);
String dateTime = result.getString(3);
messages.add(new GCMMessage(id, title, text, dateTime));
result.moveToNext();
}
L.m("Get all gcm message : " + rows);
return messages;
} catch (Exception ex) {
return null;
}
finally {
if(result != null) result.close();
if(this.mDatabase != null) this.mDatabase.close();
this.close();
}
}
}