GCM推送通知 - 需要在移动设备通知区域中按顺序查看所有推送警报。

3
如果我的提问太简单了,请谅解。目前我正在开发一个安卓应用程序,在其中我将收到多个警报(来自服务器),然后我必须按顺序查看所有的推送警报(在手机通知列表中)。
下面是我的代码。我的服务器已经成功地向我的安卓手机发送了所有的GCM推送警报通知。 但是,我只能查看最后一个被发送的推送警报(在通知列表中)。也就是说,假设我的服务器发送了两个推送警报:PUSH_A 和 PUSH_B。在我的安卓手机上,我只能查看到第二个警报:PUSH_B。
请问我做错了什么?我想在手机上按顺序查看所有的推送警报(PUSH_A 和 PUSH_B 在通知列表中)。
以下是服务器代码
Sender aGcmSender = new Sender(API_KEY); 

//Constructing message which need to be transmitted to android device.      
Message aMessage = new Message.Builder().addData("message", thePushMsg).build(); 

Result aResult = aGcmSender.send(aMessage, theRegId, 1); //Transmitting message to android device
if(aResult != null)
{
    if(aResult.getMessageId() != null)
    {
        String aCanonicalRegistrationId = aResult.getCanonicalRegistrationId();
        if(aCanonicalRegistrationId != null)
        {
            //updating aCanonicalRegistrationId to my database
        }
        itsLogger.info("Push notification successfully sent to cub of phone: "+theCubsPhone);
        itsLogger.info("Message Id:"+aResult.toString());
    }
    else 
    {  
        String error = aResult.getErrorCodeName();
        itsLogger.info("Android Push Notification: Failed to send push notification to phone" + thePushMsg);
        if (error.equals(Constants.ERROR_NOT_REGISTERED)) {    
            // application has been removed from device - unregister database
        }
    }
}

移动设备中的 GCM 代码: * AndroidManifest.xml: *

 <permission android:name="MyProjectPackage.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="MyProjectPackage.permission.C2D_MESSAGE" />

<!-- App receives GCM messages. -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <!-- GCM requires a Google account. -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <uses-permission android:name="android.permission.READ_OWNER_DATA" />
    <!-- Keeps the processor from sleeping when a message is received. -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <!-- Permission to vibrate -->
    <uses-permission android:name="android.permission.VIBRATE" />

<!-- BroadcastReceiver for push alert notifications -->
        <receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" >
          <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="MyProjectPackage" />
          </intent-filter>
        </receiver>
        <service android:name=".GCMIntentService" />

GCMIntentService:

private void sendGCMIntent(final Context theContext, String theMessage) 
    {   
    int icon = R.drawable.icon;
        long when = System.currentTimeMillis();

        NotificationManager notificationManager = (NotificationManager)
                theContext.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, theMessage, when);

        String title = theContext.getString(R.string.app_name);

        Intent notificationIntent = new Intent();
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent =
                PendingIntent.getActivity(theContext, 0, notificationIntent, 0);
        notification.setLatestEventInfo(theContext, title, theMessage, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        // Play default notification sound
        notification.defaults |= Notification.DEFAULT_SOUND;

        // Vibrate if vibrate is enabled
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notification);    
    }

请问有什么需要帮助的吗?

谢谢。


请确保您从服务器发送的每条消息都具有不同的折叠键。http://developer.android.com/google/gcm/adv.html - Vjy
2个回答

12

因为您的通知ID始终为0

notificationManager.notify(0, notification);
新的通知将会替换掉以前的通知。你只需要将 ID 改为任何数字,例如:
if (theMessage.equals(PUSH_A ))
    notificationManager.notify(0, notification);
else
    notificationManager.notify(1, notification);

谢谢回复。例如,我已经说了PUSH_A和PUSH_B。假设我的服务器发送了“n”个警报。如何让我的notificationManager列出所有“n”个警报?请帮忙。 - user915303
1
谢谢Mahmoud Hatem。你是正确的。为了列出不同id的“n”个警报,我使用了(int)Calendar.getInstance().getTimeInMillis()作为id...所以notificationManager.notify((int)Calendar.getInstance().getTimeInMillis(), notification);再次感谢。 - user915303
我认为这与折叠键有关。 - Vjy
1
当我发送多条消息时,通知栏会显示相应数量的应用图标,我想只显示一个图标和计数器。这是否可行? - Puneet

0

放置

int n_id =  new Random().nextInt();

将此n_id传递给
mNotificationManager.notify(appName, n_id, mBuilder.build());

函数 由于我已经传递了它 因此,整数ID将始终获得不同的随机数,这样每个新消息都将收到新的通知


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接