大家好,我一直在关注Sinch的这个教程:https://www.sinch.com/learn/sinch-managed-push-calling-android/,问题是GcmBroadcastReceiver似乎无法接收任何东西。
在此之前,我可以在聊天活动打开的用户之间获得即时消息。现在,我正在尝试在未打开聊天活动时收到通知。据我所知,Sinch应该负责推动,我需要做的就是接收推送通知并随心所欲地做任何事情。当我使用另一个设备向一个设备发送消息时,就推送通知而言没有任何反应。有帮助吗?非常感谢你!
我的AndroidManifest相关部分:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-feature android:name="android.hardware.camera"
android:required="true" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<permission android:name="com.myAppPackage.C2D_MESSAGE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.myAppPackage.permission.C2D_MESSAGE"/>
...
<!-- Chat Activities -->
<service android:name=".Managers.MessageService"
android:enabled="true"/>
<service android:name=".GcmIntentService" />
<receiver
android:name=".GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
<category android:name="com.myAppPackage"/>
</intent-filter>
</receiver>
MessageService
...
public void startSinchClient(String username) {
Log.d("SinchClient", "Started");
sinchClient = Sinch.getSinchClientBuilder().context(this).userId(username).applicationKey(APP_KEY)
.applicationSecret(APP_SECRET).environmentHost(ENVIRONMENT).build();
//sinchClient.setSupportCalling(true);
sinchClient.setSupportManagedPush(true);
sinchClient.addSinchClientListener(this);
sinchClient.setSupportMessaging(true);
sinchClient.setSupportActiveConnectionInBackground(true);
sinchClient.checkManifest();
sinchClient.start();
}
...
@Override
public void onClientStarted(SinchClient client) {
broadcastIntent.putExtra("success", true);
Log.d("MessageService", "onClient success");
broadcaster.sendBroadcast(broadcastIntent);
client.startListeningOnActiveConnection();
messageClient = client.getMessageClient();
}
GcmBroadcastReceiver
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("onReceive", "called!");
ComponentName comp = new ComponentName(context.getPackageName(), GcmIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
GcmIntentService
public class GcmIntentService extends IntentService implements ServiceConnection {
private Intent mIntent;
private NotificationManager mNotificationManager;
public GcmIntentService() {
super("GcmIntentService");
Log.d("GcmIntentService", "Created");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d("GcmIntentService", "onHandleIntent");
if (SinchHelpers.isSinchPushIntent(intent)) {
mIntent = intent;
connectToService();
} else {
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
}
private void connectToService() {
Log.d("GcmIntentService", "connectToService");
getApplicationContext().bindService(new Intent(this, SinchClient.class), this, Context.BIND_AUTO_CREATE);
}
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
Log.d("GcmIntentService", "onServiceConnected");
if (mIntent == null) {
return;
}if (SinchHelpers.isSinchPushIntent(mIntent)) {
SinchClient sinchService = (SinchClient) iBinder;
if (sinchService != null) {
//NotificationResult result = sinchService.relayRemotePushNotificationPayload(mIntent);
// handle result, e.g. show a notification or similar
mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, IntroActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setContentTitle("New Message!");
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(GlobalVars.NOTIFICATION_ID, mBuilder.build());
}
}GcmBroadcastReceiver.completeWakefulIntent(mIntent);
mIntent = null;
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
}
我所遵循的教程包含有错误的内容,因此我使用&#34; SinchClient&#34;而不是&#34; SinchService&#34;,它具有这些功能。另外,Sinch没有GitHub这个特定教程的例子:( 第一次发帖问题:)再次感谢!
更新
接收器现在以某种方式工作,但GcmIntentService没有响应函数startWakefulService(context, (intent.setComponent(comp)))