【问题标题】:PubNub Push messages not workingPubNub 推送消息不起作用
【发布时间】:2015-07-27 21:21:33
【问题描述】:

我正在使用 GCM 和 Pubnub 将推送通知从一台设备发送到另一台设备。 我正在遵循此处给出的代码 -

http://www.pubnub.com/blog/sending-receiving-android-push-notifications-with-gcm-google-cloud-messaging/

我正在接收从其他设备发送的数据,并且也可以显示它。但通知(在通知栏中)不起作用。

收到数据后,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 注册/注销等工作正常。请让我知道问题到底出在哪里。谢谢。

【问题讨论】:

  • 您好,Nis 一定要 ping support@pubnub.com 以及您的问题,我们有一个团队可以开始审查您的 xml 清单和配置。
  • Nis,只是检查一下您是否确实发帖支持。如果没有,请这样做。
  • 感谢@CraigConover!发现了问题。这是一个愚蠢的错误。

标签: android push-notification google-cloud-messaging android-service pubnub


【解决方案1】:

我发现了问题。

我错误地将标签声明在Manifest的标签外面。 所以 Receiver 没有被注册。

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多