Quickblox 应用关闭时推送
Quickblox Push when App is Closed
我正在开发一个应用程序,使用 Quickblox 创建与用户的聊天。我已经设法在应用程序中获得推送服务,即使它在后台也是如此,但是一旦应用程序被强制关闭,就不会收到推送。
我的 android 设备正在 GMC QB 服务上注册,令牌出现并且注册没有问题。
此外,我什至尝试在 Quickblox 上使用 Push Admin,但 Push 也不起作用。
源代码:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<permission
android:name="com.myapp.app.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.myapp.app.permission.C2D_MESSAGE" />
<receiver
android:name="com.quickblox.chat.push.QBGcmBroadcastReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.myapp.app" />
</intent-filter>
</receiver>
<service android:name="com.quickblox.chat.push.QBGcmIntentServiceNewVersion"/>
<!-- CUSTOM -->
<service
android:name="com.myapp.app.push.CPInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<service
android:name="com.myapp.app.push.CPRegistrationIntentService"
android:exported="false" />
还有我的 BroadcastReceiver:
public class QBGcmBroadcastReceiver extends WakefulBroadcastReceiver {
public static int QBNotificationID = 4444;
@Override
public void onReceive(Context context, Intent intentReceived) {
Log.e(CPApplication.TAG, "QBGcmBroadcastReceiver -> Push RECIBIDA");
Log.e(CPApplication.TAG, "APP is ON ?() -> " + CPApplication.appIsON);
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(), QBGcmIntentServiceNewVersion.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intentReceived.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
// App is not running
if (!CPApplication.appIsON) {
// Is New Message Push enabled ?
boolean isPushNewMessage = CPApplication.getSharedPrefs().getBoolean(CPConstants.SP_PUSH_NEW_MESSAGE, true);
// Is the user registered
boolean rememberPassword = CPApplication.getSharedPrefs().getBoolean(CPConstants.SP_REMEMBER_PASSWORD, false);
if (rememberPassword && isPushNewMessage) {
// Notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(CPApplication.getInstance());
builder.setSmallIcon(R.drawable.push_icon)
.setTicker("Nuevo mensaje")
.setAutoCancel(true)
.setContentTitle(CPApplication.getInstance().getString(R.string.app_name))
.setContentText("Tiene un mensaje nuevo");
// Is New Message Sound enabled ?
boolean isSoundNewMessage = CPApplication.getSharedPrefs().getBoolean(CPConstants.SP_SOUND_NEW_MESSAGE, true);
if (isSoundNewMessage) {
builder.setDefaults(Notification.DEFAULT_SOUND);
}
// Intent
Intent intent = new Intent(CPApplication.getInstance(), SplashActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(CPApplication.getInstance(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Notification
Notification note = builder.build();
// Will show lights and make the notification disappear when the presses it
note.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
NotificationManager manager = (NotificationManager) CPApplication.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(QBNotificationID, note);
}
}
}
}
我还创建了一个意向服务 class:
public class QBGcmIntentServiceNewVersion extends IntentService {
public static int QBNotificationID = 4444;
public QBGcmIntentServiceNewVersion() {
super(QBPushConstants.GCM_INTENT_SERVICE);
}
@Override
protected void onHandleIntent(Intent intentReceived) {
Log.i(CPApplication.TAG, "QBGcmIntentServiceNewVersion :: onHandleIntent");
// App is not running
// if (!CPApplication.appIsON) {
// Is New Message Push enabled ?
boolean isPushNewMessage = CPApplication.getSharedPrefs().getBoolean(CPConstants.SP_PUSH_NEW_MESSAGE, true);
// Is the user registered
boolean rememberPassword = CPApplication.getSharedPrefs().getBoolean(CPConstants.SP_REMEMBER_PASSWORD, false);
if (rememberPassword && isPushNewMessage) {
// Notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(CPApplication.getInstance());
builder.setSmallIcon(R.drawable.push_icon)
.setTicker("Nuevo mensaje")
.setAutoCancel(true)
.setContentTitle(CPApplication.getInstance().getString(R.string.app_name))
.setContentText("Tiene un mensaje nuevo");
// Is New Message Sound enabled ?
boolean isSoundNewMessage = CPApplication.getSharedPrefs().getBoolean(CPConstants.SP_SOUND_NEW_MESSAGE, true);
if (isSoundNewMessage) {
builder.setDefaults(Notification.DEFAULT_SOUND);
}
// Intent
Intent intent = new Intent(CPApplication.getInstance(), SplashActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(CPApplication.getInstance(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Notification
Notification note = builder.build();
// Will show lights and make the notification disappear when the presses it
note.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
NotificationManager manager = (NotificationManager) CPApplication.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(QBNotificationID, note);
}
// }
}
}
谢谢
发布您的权限也可能有帮助,但您没有将接收器中的名称更改为您的包名称:
android:name="com.quickblox.chat.push.QBGcmBroadcastReceiver"
尝试将其更改为:
android:name="com.myapp.app.QBGcmBroadcastReceiver"
如果它不起作用,请编辑您的问题并添加您的权限。希望对你有帮助。
抱歉,我输入的是令牌而不是设备 ID,因此它在 QB 管理员上显示为新注册的设备,但信息错误,这就是问题所在。我使用的是 QB
的新方法
new SendPushTokenToQBTask(token, deviceId).execute();
而不是
new SendPushTokenToQBTask(deviceId, token).execute()
还是谢谢你
我正在开发一个应用程序,使用 Quickblox 创建与用户的聊天。我已经设法在应用程序中获得推送服务,即使它在后台也是如此,但是一旦应用程序被强制关闭,就不会收到推送。 我的 android 设备正在 GMC QB 服务上注册,令牌出现并且注册没有问题。 此外,我什至尝试在 Quickblox 上使用 Push Admin,但 Push 也不起作用。
源代码:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<permission
android:name="com.myapp.app.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.myapp.app.permission.C2D_MESSAGE" />
<receiver
android:name="com.quickblox.chat.push.QBGcmBroadcastReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.myapp.app" />
</intent-filter>
</receiver>
<service android:name="com.quickblox.chat.push.QBGcmIntentServiceNewVersion"/>
<!-- CUSTOM -->
<service
android:name="com.myapp.app.push.CPInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<service
android:name="com.myapp.app.push.CPRegistrationIntentService"
android:exported="false" />
还有我的 BroadcastReceiver:
public class QBGcmBroadcastReceiver extends WakefulBroadcastReceiver {
public static int QBNotificationID = 4444;
@Override
public void onReceive(Context context, Intent intentReceived) {
Log.e(CPApplication.TAG, "QBGcmBroadcastReceiver -> Push RECIBIDA");
Log.e(CPApplication.TAG, "APP is ON ?() -> " + CPApplication.appIsON);
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(), QBGcmIntentServiceNewVersion.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intentReceived.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
// App is not running
if (!CPApplication.appIsON) {
// Is New Message Push enabled ?
boolean isPushNewMessage = CPApplication.getSharedPrefs().getBoolean(CPConstants.SP_PUSH_NEW_MESSAGE, true);
// Is the user registered
boolean rememberPassword = CPApplication.getSharedPrefs().getBoolean(CPConstants.SP_REMEMBER_PASSWORD, false);
if (rememberPassword && isPushNewMessage) {
// Notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(CPApplication.getInstance());
builder.setSmallIcon(R.drawable.push_icon)
.setTicker("Nuevo mensaje")
.setAutoCancel(true)
.setContentTitle(CPApplication.getInstance().getString(R.string.app_name))
.setContentText("Tiene un mensaje nuevo");
// Is New Message Sound enabled ?
boolean isSoundNewMessage = CPApplication.getSharedPrefs().getBoolean(CPConstants.SP_SOUND_NEW_MESSAGE, true);
if (isSoundNewMessage) {
builder.setDefaults(Notification.DEFAULT_SOUND);
}
// Intent
Intent intent = new Intent(CPApplication.getInstance(), SplashActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(CPApplication.getInstance(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Notification
Notification note = builder.build();
// Will show lights and make the notification disappear when the presses it
note.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
NotificationManager manager = (NotificationManager) CPApplication.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(QBNotificationID, note);
}
}
}
}
我还创建了一个意向服务 class:
public class QBGcmIntentServiceNewVersion extends IntentService {
public static int QBNotificationID = 4444;
public QBGcmIntentServiceNewVersion() {
super(QBPushConstants.GCM_INTENT_SERVICE);
}
@Override
protected void onHandleIntent(Intent intentReceived) {
Log.i(CPApplication.TAG, "QBGcmIntentServiceNewVersion :: onHandleIntent");
// App is not running
// if (!CPApplication.appIsON) {
// Is New Message Push enabled ?
boolean isPushNewMessage = CPApplication.getSharedPrefs().getBoolean(CPConstants.SP_PUSH_NEW_MESSAGE, true);
// Is the user registered
boolean rememberPassword = CPApplication.getSharedPrefs().getBoolean(CPConstants.SP_REMEMBER_PASSWORD, false);
if (rememberPassword && isPushNewMessage) {
// Notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(CPApplication.getInstance());
builder.setSmallIcon(R.drawable.push_icon)
.setTicker("Nuevo mensaje")
.setAutoCancel(true)
.setContentTitle(CPApplication.getInstance().getString(R.string.app_name))
.setContentText("Tiene un mensaje nuevo");
// Is New Message Sound enabled ?
boolean isSoundNewMessage = CPApplication.getSharedPrefs().getBoolean(CPConstants.SP_SOUND_NEW_MESSAGE, true);
if (isSoundNewMessage) {
builder.setDefaults(Notification.DEFAULT_SOUND);
}
// Intent
Intent intent = new Intent(CPApplication.getInstance(), SplashActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(CPApplication.getInstance(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Notification
Notification note = builder.build();
// Will show lights and make the notification disappear when the presses it
note.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
NotificationManager manager = (NotificationManager) CPApplication.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(QBNotificationID, note);
}
// }
}
}
谢谢
发布您的权限也可能有帮助,但您没有将接收器中的名称更改为您的包名称:
android:name="com.quickblox.chat.push.QBGcmBroadcastReceiver"
尝试将其更改为:
android:name="com.myapp.app.QBGcmBroadcastReceiver"
如果它不起作用,请编辑您的问题并添加您的权限。希望对你有帮助。
抱歉,我输入的是令牌而不是设备 ID,因此它在 QB 管理员上显示为新注册的设备,但信息错误,这就是问题所在。我使用的是 QB
的新方法new SendPushTokenToQBTask(token, deviceId).execute();
而不是
new SendPushTokenToQBTask(deviceId, token).execute()
还是谢谢你