PubNub 推送消息不起作用
PubNub Push messages not working
我正在使用 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());
}
}
清单文件也可能有问题。这是我的清单文件 -
<?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 中的其他代码(在 link 中给出)如 GCM 注册/取消注册等工作正常。请让我知道问题到底出在哪里。谢谢。
我发现了问题。
我错误地将 标签声明在 Manifest 的 标签之外。
所以 Receiver 没有注册。
谢谢。
我正在使用 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());
}
}
清单文件也可能有问题。这是我的清单文件 -
<?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 中的其他代码(在 link 中给出)如 GCM 注册/取消注册等工作正常。请让我知道问题到底出在哪里。谢谢。
我发现了问题。
我错误地将
谢谢。