3

如果我的问题太简单,请道歉。

目前我正在开发一个 android 应用程序,其中我将收到多个警报(来自服务器),并且我必须一个接一个地查看所有推送警报(在移动通知列表中)。

我在下面给出了我的代码。我的服务器正在成功地将所有 GCM 推送警报通知发送到我的 android 手机。

但是,我只能查看最后发送的推送警报(在通知列表中)。也就是说,考虑我的服务器正在发送 2 个推送警报: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);    
    }

请提供任何帮助。

谢谢你。

4

2 回答 2

12

因为您的通知 ID 始终为 0

notificationManager.notify(0, notification);

新通知将替换为以前的通知。只是您必须将 id 更改为这样的任何数字:

if (theMessage.equals(PUSH_A ))
    notificationManager.notify(0, notification);
else
    notificationManager.notify(1, notification);
于 2013-01-08T15:01:38.560 回答
0

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

把这个n_id传给

mNotificationManager.notify(appName, n_id, mBuilder.build());

函数,因为我已经传递了它,因此整数 id 总是会得到不同的随机数,这种方式总是会收到每条新消息的新通知

于 2016-02-10T12:17:16.773 回答