我正在使用GCM和Pubnub将推送通知从一个设备发送到另一个设备。 我按照这里给出的代码 -
我正在接收从其他设备发送的数据,并且还能够显示它。但通知(在通知栏中)无效。
收到数据后,Textview会更改为显示数据,但通知栏中没有任何内容。
以下是我的广播接收器和GCM意向服务的代码(我猜这里可能有问题) -
广播接收器
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
Log.i("NIS", "Inside Brocast receiver");
}
}
服务
public class GcmIntentService extends IntentService {
private static final int NOTIFICATION_ID = 12345;
public GcmIntentService() {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent)
{
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty() && GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType))
{
sendNotification("Received: " + extras.toString());
Log.i("NIS", "Inside handle intent");
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg)
{
NotificationManager mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, ChatsList.class), 0);
Log.i("NIS", "send notification notification biulder");
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.kohls)
.setContentTitle("PubNub GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
Manifest文件中也可能存在问题。这是我的清单文件 -
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.example.nishant.tkmabft.kconnect20.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.example.nishant.tkmabft.kconnect20.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".ChatsList"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ChatActivity"
android:label="@string/title_activity_chat" >
</activity>
</application>
<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="your.package.name" />
</intent-filter>
</receiver>
<service android:name=".GcmIntentService" />
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
Activity中的其他代码(在提供的链接中给出),如GCM注册/取消注册等工作正常。请让我知道问题究竟在哪里。谢谢。
答案 0 :(得分:2)
我发现了问题。
我错误地声明了&lt;接收器&gt;标签外&lt;申请&gt;清单中的标签。 所以Receiver没有注册。
感谢。