在android中使用GCM服务

时间:2015-02-17 06:11:20

标签: java android google-cloud-messaging

我在做什么:

  • 我正在使用推送通知来接收通知。
  • 我使用了以下代码
  • 我还在清单
  • 中声明了必要的权限

发生了什么:

  • 我可以收到通知
  • 但是当新通知出现时,旧的通知会被替换

我要做的是:

  • 说收到第一个通知
  • 当第二个通知出现时,不应更换 相反,两者都应该显示

GCMNotificationIntentService.java

public class GCMNotificationIntentService extends IntentService {
    public static final int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;

    public GCMNotificationIntentService() {
        super("GcmIntentService");
    }

    public static final String TAG = "GCMNotificationIntentService";

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {
            if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                //sendNotification("Send error: " + extras.toString());
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
                //sendNotification("Deleted messages on server: "+ extras.toString());
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                //sendNotification("Message Received from Google GCM Server: "+ extras.get(Keys.MSG_KEY));
            }
        }
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }
}

GcmBroadcastReceiver.java

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) {
            handleMessage(context, intent);
        }

        ComponentName comp = new ComponentName(context.getPackageName(),
                GCMNotificationIntentService.class.getName());
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
        /*Toast.makeText(context, "notification_Recieved", Toast.LENGTH_LONG)
                .show();*/
    }

    private void handleMessage(Context context, Intent intent) {

        if(intent.getExtras()!=null){

            Bundle message = intent.getExtras();
            String s= message.getString("the_message");
            final Intent emptyIntent = new Intent(context,ActMain.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 1234, emptyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.notification_logo)
                    .setContentTitle(context.getResources().getString(R.string.screenname_apptitle))
                    .setContentText(s)
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent);

             // Set Vibrate, Sound and Light            
            int defaults = 0;
            defaults = defaults | Notification.DEFAULT_SOUND | Notification.FLAG_AUTO_CANCEL;
            mBuilder.setDefaults(defaults);

            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(1234, mBuilder.build());

            /*Toast.makeText(context.getApplicationContext(),
                    "\n message : " + message, 1).show();*/

            NotificationManager objNotfManager = (NotificationManager) context
                    .getApplicationContext().getSystemService(
                            Context.NOTIFICATION_SERVICE);
        }
    }
}

3 个答案:

答案 0 :(得分:1)

它正在替换旧的,因为通知ID对于所有通知都是相同的。因此,当您尝试添加新通知时,它会替换旧通知,而不是添加新通知。

notificationManager.notify(1234, mBuilder.build());

此行是问题,您发送1234作为所有通知的通知ID,将此行替换为 notificationManager.notify(uniqueId, mBuilder.build());

如果您需要针对不同推送的不同通知添加不同的通知ID。 您也可以将收件箱模式设置为通知,以便在gmail下看到一组通知,请参阅此link

为了更好地了解通知,请参阅this

答案 1 :(得分:0)

尝试更改此

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(1234, mBuilder.build());

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(System.currentTimeMillis(), mBuilder.build());

它应该有一个唯一的编号。希望这会有所帮助

答案 2 :(得分:0)

Date date = new Date();
int notification_id = (int) date.getTime();
notificationManager.notify(notification_id, notification);

使用时间戳ID而不是通知ID的最终值