簡體   English   中英

如何在Android + GCM中使用聲音+自定義應用程序圖標獲取推送通知

[英]How to get a push notification with sound + custom app icon in Android + GCM

我正在為學校項目構建聊天應用程序。 到目前為止一切正常。 這樣我就可以通過GCM獲得推送通知。 但是這些只是默默顯示。 您能幫我弄清楚如何制作聲音(默認聲音足夠好)和我自己的應用程序圖標嗎?

編輯:無法使用Firebase。 我們必須在項目中使用GCM。

在下面,您可以看到我的代碼來處理通知。 謝謝。

    NotificationHandler notificationHandler = new NotificationHandler(getApplicationContext());

    //If the app is in foreground
    if (!NotificationHandler.isAppIsInBackground(getApplicationContext())) {
        //Sending a broadcast to the chatroom to add the new message
        LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
    } else {
        //If app is not in foreground displaying push notification
        notificationHandler.showNotificationMessage(title, message);
    }

試試這個,對我有用

protected void displayNotification(Context context, String[] strContent) {
        try {
            int numMessages = 0;

            /* Invoking the default notification service */
            NotificationCompat.Builder  mBuilder = new NotificationCompat.Builder(this);

            mBuilder.setContentTitle("New "+strContent[0].trim());
            mBuilder.setContentText("You've received new "+strContent[0].trim());
            mBuilder.setTicker("New "+strContent[0].trim()+" Alert!");
            mBuilder.setSmallIcon(R.drawable.ic_notif);
            mBuilder.setAutoCancel(true);
            mBuilder.setColor(context.getResources().getColor(R.color.ColorPrimary));
            mBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);
            mBuilder.setDefaults(Notification.DEFAULT_ALL);
            mBuilder.setVibrate(new long[] {1000, 1000, 1000, 1000, 1000});         

            mBuilder.setLights(Color.RED, 3000, 3000);
         mBuilder.setNumber(++numMessages);

        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

            String[] events = new String[strContent.length-1];
            for(int i=0; i<strContent.length-1; i++){
                events[i] = new String(strContent[i+1].trim());
            }

            inboxStyle.setBigContentTitle("New "+strContent[0].trim()+" received!");

            for (int i=0; i < events.length; i++) {
                inboxStyle.addLine(events[i]);
            }

            mBuilder.setStyle(inboxStyle);

            /* Creates an explicit intent for an Activity in your app */
            Intent resultIntent = new Intent(this, HomeActivity.class);


            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            stackBuilder.addParentStack(HomeActivity.class);

            /* Adds the Intent that starts the Activity to the top of the stack */
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent =stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

            mBuilder.setContentIntent(resultPendingIntent);
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            /* notificationID allows you to update the notification later on. */
            mNotificationManager.notify(9999, mBuilder.build());
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

暫無
暫無

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

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