簡體   English   中英

在Android中使用GCM服務

[英]Using GCM service in android

我在做什么:

  • 我正在使用推送通知來接收通知。
  • 我用下面的代碼
  • 我還在清單中聲明了必要的權限

怎么了:

  • 我能夠收到通知
  • 但是當新的通知到來時,舊的被替換

我正在嘗試做的是:

  • 說收到第一個通知
  • 當第二個通知過期時,不應替換一個通知,而應同時顯示兩個通知

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);
        }
    }
}

它正在替換舊的通知,因為所有通知的通知ID都相同。 因此,當您嘗試添加新的通知時,它將替換舊的通知,而不是添加新的通知。

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

這行是問題所在,您要發送1234作為所有通知的通知ID,請用notificationManager.notify(uniqueId, mBuilder.build());替換此行notificationManager.notify(uniqueId, mBuilder.build());

如果您希望不同的推送通知不同,請添加不同的通知ID。 您還可以將收件箱模式設置為通知,以便在一組通知下可以看到一組通知,就像gmail看到此鏈接一樣

為了更好地了解通知,請參閱

嘗試改變這個

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());

它應該有一個唯一的數字。 希望這會有所幫助

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

使用時間戳ID代替通知ID的最終值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM