当前位置: 首页 > 知识库问答 >
问题:

未收到推送通知,quickblox,android

岳玉堂
2023-03-14

因此,我试图使用GCM和quickBlox实现推送通知,这样我就可以使用quickBlox订阅推送通知,当消息发送时,不会收到通知。

public class MainActivity extends AppCompatActivity {

static final String APP_ID = "45535";
static final String AUTH_KEY = "L-kz28SrxuSrn23";
static final String AUTH_SECRET = "zJX63sgj9Nm3qMB";
static final String ACCOUNT_KEY = "sor71FWvtVnx7d9JdTyd";

private GoogleCloudMessaging googleCloudMessaging;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    QBSettings.getInstance().init(getApplicationContext(), APP_ID, AUTH_KEY, AUTH_SECRET);
    QBSettings.getInstance().setAccountKey(ACCOUNT_KEY);

    if (checkPlayServices()) {
        // Start IntentService to register this application with GCM.
        Intent intent = new Intent(this, RegistrationIntentService.class);
        startService(intent);
    }


    // Create quickblox session with user
    QBAuth.createSession("laddu", "chowmein", new QBEntityCallback<QBSession>() {
        @Override
        public void onSuccess(QBSession session, Bundle params) {
            //request Registration ID
            String registrationId = "16441570";

            // Subscribe to Push Notifications
            subscribeToPushNotifications(registrationId);

        }

        @Override
        public void onError(QBResponseException errors) {

        }
    });


    EditText text;
    text = (EditText) findViewById(R.id.editText);

    Button send;
    send = (Button) findViewById(R.id.button);
    send.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            StringifyArrayList<Integer> userIds = new StringifyArrayList<Integer>();
            //add different user ID
            userIds.add(16335930);
            userIds.add(16441570);

            QBEvent event = new QBEvent();
            event.setUserIds(userIds);
            event.setEnvironment(QBEnvironment.DEVELOPMENT);
            event.setNotificationType(QBNotificationType.PUSH);
            event.setPushType(QBPushType.GCM);

            //HashMap<String, String> data = new HashMap<String, String>();
            //data.put("data.message", "Hello");
            //data.put("data.type", "welcome message");

            event.setMessage("Hello");

            QBPushNotifications.createEvent(event, new QBEntityCallback<QBEvent>() {
                @Override
                public void onSuccess(QBEvent qbEvent, Bundle args) {
                    // sent
                }

                @Override
                public void onError(QBResponseException errors) {

                }
            });
        }
    });
}

/**
 * Check the device to make sure it has the Google Play Services APK. If
 * it doesn't, display a dialog that allows users to download the APK from
 * the Google Play Store or enable it in the device's system settings.
 */
private boolean checkPlayServices() {
    GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
    int resultCode = apiAvailability.isGooglePlayServicesAvailable(this);
    if (resultCode != ConnectionResult.SUCCESS) {
        if (apiAvailability.isUserResolvableError(resultCode)) {
            //apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST)
                    //.show();
        } else {
            //Log.i(TAG, "This device is not supported.");
            finish();
        }
        return false;
    }
    return true;
}


public void subscribeToPushNotifications(String registrationID) {
    QBSubscription subscription = new QBSubscription(QBNotificationChannel.GCM);
    subscription.setEnvironment(QBEnvironment.DEVELOPMENT);
    //
    String deviceId;
    final TelephonyManager mTelephony = (TelephonyManager) getSystemService(
            Context.TELEPHONY_SERVICE);
    if (mTelephony.getDeviceId() != null) {
        deviceId = mTelephony.getDeviceId(); //*** use for mobiles
    } else {
        deviceId = mTelephony.getDeviceId();
        //deviceId = Settings.Secure.getString(activity.getContentResolver(),
           //     Settings.Secure.ANDROID_ID); //*** use for tablets
    }
    subscription.setDeviceUdid(deviceId);
    Toast.makeText(MainActivity.this, deviceId, Toast.LENGTH_SHORT).show();
    //
    subscription.setRegistrationID(registrationID);
    //
    QBPushNotifications.createSubscription(subscription, new QBEntityCallback<ArrayList<QBSubscription>>() {

        @Override
        public void onSuccess(ArrayList<QBSubscription> subscriptions, Bundle args) {

        }

        @Override
        public void onError(QBResponseException error) {

        }
    });
}

公共类MyGcmListenerService扩展了GcmListenerService{

private static final String TAG = "MyGcmListenerService";

@Override
public void onMessageReceived(String from, Bundle data) {
    String message = data.toString();
    Log.d(TAG, "From: " + from);
    Log.d(TAG, "Message: " + message);

    if (from.startsWith("/topics/")) {
        // message received from some topic.
    } else {
        // normal downstream message.
    }

    sendNotification(message);
    // [END_EXCLUDE]
}

private void sendNotification(String message) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setContentTitle("GCM Message")
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

}

共有1个答案

贝浩歌
2023-03-14

您可以查看下一个:

  1. 检查推送证书:管理面板->推送通知->设置->APNS、GCM等
  2. 检查订阅:管理面板->推送通知->订阅。您还可以检查用户的订阅:管理面板->用户->“您的”用户
  3. 从管理面板发送推送->推送通知->发送http://quickblox.com/developers/simplesample-messages_users-android#send_push_notifications_from_admin_panel
 类似资料:
  • 当我的应用程序处于打开状态时,我正在通过onMessageReceived(Remotemessage mesg)获得推送通知。如果我的应用程序处于Kilded状态,我将收到推送通知,但不是从onMessageReceived()获得的。 意思是,在收到推送通知后,根据通知中的数据,我需要重定向页面,当通知点击时。如果应用程序在前景工作良好。当我杀人的时候,我在托盘上收到通知,但当我点击通知时,它

  • 嗨,我正在为Windows Phone8开发。我需要发送和接收贴图推送通知使用PHP。我的设备上未收到平铺推送通知。我得到了下面给出的错误。有人能告诉我为什么我要面对这个问题吗? 错误: HTTP/1.1 200 OK缓存-控件:专用服务器:Microsoft-IIS/7.5 X-DeviceConnectionStatus:已连接X-NotificationStatus:已取消X-Subscri

  • Samsung正在成功获取应用程序在后台或前台被杀死, 我的目标是API27,这是我的代码 分级

  • 我没有收到Android推送通知。我能够 < li >注册设备 < li >接收设备标识符 < li >将通知消息推送到GCM 我成功进入通知发送事件,但我的设备上没有收到任何消息。成功的推送代码如下:

  • 我是一名php开发人员。我正在为iPhone实现推送通知模块。为此,我使用php进行服务器端实现。虽然我收到“已连接到APNS {”aps“:{”警报“:”嗨推送“,”徽章“:1,”声音“:”默认“}}消息已成功发送”消息,但iPhone没有收到任何通知。我的 php 代码是这样的: ? 目标c代码是这样的:

  • 我有一个React原生应用程序(在iOS上测试),正在尝试合并推送通知。我正在使用以下模块:https://www.npmjs.com/package/react-native-firebase-push-notifications. 我尝试运行示例应用程序代码,并能够获得(1)消息令牌和(2)从我的设备成功获得权限。 我正在尝试从Firebase发送测试通知,并且正在使用我设备的令牌。但是,触发

  • 我认为通过使用,我可以捕获请求正文,但事实是它什么也没有捕获。如果我理解正确,我应该收到一个结构在这里详细说明的变更资源。是我做错了什么,还是我理解错了?我很感激任何可以给予的洞察力和提前感谢!

  • 我们正在使用解析存储聊天消息,我们正在使用解析通知。 对于iOS,我们这样做是为了在Parse的安装表中创建条目…它在parse的安装表中创建条目,我认为这是接收通知所必须的。 我没有收到来自解析的通知,我认为这是没有收到通知的原因… 事实是这样的吗? 有谁能帮我走上正路吗? 更新 现在收到通知但仍有些疑虑。我在用户登录时执行此操作。我有放条件,如果用户没有创建,那么只有创建,否则不。我没有把它加