从Android中的GCM Intent Service不断获取通知 [英] Getting notification endlessly from GCM Intent Service in android

查看:79
本文介绍了从Android中的GCM Intent Service不断获取通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个活动,该活动在发生通知时会刷新,但是我面临的一个问题是,当通知到来时,它会继续发送无尽的相同通知。之前它运行良好,但是我做了一些更改,并且做到了。请帮我。我在其中附加我的代码。

I have created a activity which got refreshes while a notification occurs but I am facing one problem which is that when a notification came it continues to send endless same notification. Earlier it was working fine but I made some changes and this had done. Please help me. I am attaching my code with it.

GCM IntentService类的代码

         @Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    String msgg = intent.getStringExtra("message");

     final ResultReceiver receiver = intent.getParcelableExtra("receiver");
    Bundle bundle = new Bundle();
    if (!extras.isEmpty()) {

        if (GoogleCloudMessaging.
                MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {

            sendNotification(this, msgg);


        } else if (GoogleCloudMessaging.
                MESSAGE_TYPE_DELETED.equals(messageType)) {

            sendNotification(this, msg);
            updateMyActivity(this,msgg);
            bundle.putString("result", msg);
            receiver.send(STATUS_FINISHED, bundle);

        } else if (GoogleCloudMessaging.
                MESSAGE_TYPE_MESSAGE.equals(messageType)) {
            updateMyActivity(this,msgg);
            sendNotification(this, msg);



        }


}

发送通知代码

 private void sendNotification(Context context, String message) {

    Intent resultIntent;

    int icon = R.mipmap.ic_launcher;
    long when = System.currentTimeMillis();
    NotificationCompat.Builder nBuilder;
    Uri alarmSound = RingtoneManager
            .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);


    NotificationCompat.BigPictureStyle notiStyle = new
            NotificationCompat.BigPictureStyle();
    notiStyle.setBigContentTitle("afewtaps");
    notiStyle.setSummaryText(message);


    nBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(icon)
            .setContentTitle("afewtaps")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .setLights(Color.BLUE, 500, 500).setContentText(message)
            .setAutoCancel(true).setTicker("Notification from afewtaps")
            .setSound(alarmSound);



    resultIntent = new Intent(context,
            LoginActivity.class);
    resultIntent.putExtra("message", message);

    resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);


    PendingIntent resultPendingIntent = PendingIntent.getActivity(context,
            notify_no, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    // Show the max number of notifications here
    if (notify_no < 9) {
        notify_no = notify_no + 1;
    } else {
        notify_no = 0;
    }
    nBuilder.setContentIntent(resultPendingIntent);

    NotificationManager nNotifyMgr = (NotificationManager) context
            .getSystemService(context.NOTIFICATION_SERVICE);



    nNotifyMgr.notify(notify_no + 2, nBuilder.build());


}

发送消息进行广播

    // This function will create an intent. This intent must take as parameter the "unique_name" that you registered your activity with
static void updateMyActivity(Context context, String message) {

    Intent intent = new Intent("com.google.android.c2dm.intent.RECEIVE");

    //put whatever data you want to send, if any
    intent.putExtra("message", message);

    //send broadcast
    context.sendBroadcast(intent);
}

这将结束意图类的代码。

现在我的活动代码

         @Override
public void onResume() {
    super.onResume();
   // connectToDatabase();
    getActivity().registerReceiver(mMessageReceiver, new IntentFilter("com.google.android.c2dm.intent.RECEIVE"));
}

//Must unregister onPause()
@Override
public void onPause() {
    super.onPause();
    getActivity().unregisterReceiver(mMessageReceiver);
}


//This is the handler that will manager to process the broadcast intent
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        // Extract data included in the Intent
        String message = intent.getStringExtra("message");

        //do other stuff here

        connectToDatabase();


    }
};


推荐答案

好的。因此,您的代码正在发生的事情是,您正在向广播接收器发送广播,并强制再次调用 onHandleIntent ,结果调用了updateActivity方法,该方法再次广播和循环不断进行。

Okay. So what is happening with your code is that you are sending a broadcast to your Receiver as well forcing the onHandleIntent to be invoked again, which in result calls the updateActivity method, which again broadcasts and loop continues endlessly.

在您的 updateMyActivity 方法中,请更改以下内容:

In your updateMyActivity method, please change this:

Intent intent = new Intent("com.google.android.c2dm.intent.RECEIVE");

Intent intent = new Intent("myMessage");

罪魁祸首是

com.google.android.c2dm.intent.RECEIVE

此外,在您的活动的 onResume 方法中,请更改 TAG myMessage

Also, in your onResume method of the activity, please change the TAG to myMessage:

getActivity().registerReceiver(mMessageReceiver, new IntentFilter("myMessage"))

这篇关于从Android中的GCM Intent Service不断获取通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆