使用GCM和Django处理Android中的推送通知

时间:2016-08-30 09:48:18

标签: android django android-studio push-notification google-cloud-messaging

我是Android编程的新手,我的应用程序中有两种类型的推送通知(例如,一个用于通知,另一个用于新闻),我如何区分这些通知以及如何通过以下方式打开不同的活动?单独点击它们?

Random random = new Random();
int notifyID = random.nextInt(9999 - 1000) + 1000;
NotificationCompat.Builder builder;

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


@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)) {

            if (ApplicationConstants.MSG_KEY.equals("message")) {
                sendNotification("Notice: " + extras.get(ApplicationConstants.MSG_KEY));
            }
        }
    }
    GcmBroadcastReceiver.completeWakefulIntent(intent);
}

private void sendNotification(String msg) {

    Intent resultIntent = new Intent(this, NoticeActivity.class);
    resultIntent.putExtra("msg", msg);
    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder mNotifyBuilder;
    NotificationManager mNotificationManager;

    Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_icon);

    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    mNotifyBuilder = new NotificationCompat.Builder(this)
            .setContentTitle("Title")
            .setContentText("You've received a new message.")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setAutoCancel(true)
            .setLargeIcon(largeIcon)
            .setPriority(Notification.PRIORITY_HIGH);

    /* Notification Icon as per API level*/

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mNotifyBuilder.setSmallIcon(R.drawable.icon_s);
        //    mNotifyBuilder.setColor(getResources().getColor(R.color.white));
    } else {
        mNotifyBuilder.setSmallIcon(R.mipmap.ic_icon);
    }

    // Set pending intent
    mNotifyBuilder.setContentIntent(resultPendingIntent);

    // Set Vibrate, Sound and Light
    int defaults = 0;
    defaults = defaults | Notification.DEFAULT_LIGHTS;
    defaults = defaults | Notification.DEFAULT_VIBRATE;
    defaults = defaults | Notification.DEFAULT_SOUND;

    mNotifyBuilder.setDefaults(defaults);
    // Set the content for Notification
    mNotifyBuilder.setContentText(msg);
    // Set autocancel
    mNotifyBuilder.setAutoCancel(true);
    // Post a notification
    mNotificationManager.notify(notifyID, mNotifyBuilder.build());
}

4 个答案:

答案 0 :(得分:0)

对于此功能,您必须使用不同的通知ID生成2个不同的通知。

让我解释一下

为通知ID定义两个int变量。

final int NOTIFICATION_NOTICE = 0;
final int NOTIFICATION_NEWS = 1;

见下面的代码。

通知

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

Intent noticeIntent = new Intent(context, NoticeActivity.class);   // Activity Name as you want to Open on click

noticeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
        | Intent.FLAG_ACTIVITY_SINGLE_TOP |  Intent.FLAG_AUTO_CANCEL);

PendingIntent intent = PendingIntent.getActivity(context, 0,
        noticeIntent, 0);

notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(NOTIFICATION_NOTICE, notification);   // Notification id for differentiate Notification

用于新闻通知

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

Intent newsIntent = new Intent(context, NewsActivity.class);     // Activity Name as you want to Open on click

newsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
        | Intent.FLAG_ACTIVITY_SINGLE_TOP |  Intent.FLAG_AUTO_CANCEL);

PendingIntent intent = PendingIntent.getActivity(context, 0,
        newsIntent, 0);

notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(NOTIFICATION_NEWS , notification);     // Notification id for differentiate Notification

修改

public class GcmIntentService extends IntentService {

   final int NOTIFICATION_NOTICE = 0;
   final int NOTIFICATION_NEWS = 1;
   NotificationCompat.Builder builder;

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


@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)) {

            if (ApplicationConstants.MSG_KEY.equals("message")) {
                sendNotification("Notice: " + extras.get(ApplicationConstants.MSG_KEY), extras.get("notificationType"));
            }
        }
    }
    GcmBroadcastReceiver.completeWakefulIntent(intent);
}

private void sendNotification(String msg, String type) {

    if(type.equalsignoreCase("Notice")){
     NotificationCompat.Builder mNotifyBuilder;
     NotificationManager mNotificationManager;

     Intent resultIntent = new Intent(this, NoticeActivity.class);
     resultIntent.putExtra("msg", msg);
     PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_ONE_SHOT);

     Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_icon);

     mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

     // Set Vibrate, Sound and Light
     int defaults = 0;
     defaults = defaults | Notification.DEFAULT_LIGHTS;
     defaults = defaults | Notification.DEFAULT_VIBRATE;
     defaults = defaults | Notification.DEFAULT_SOUND;


     mNotifyBuilder = new NotificationCompat.Builder(this)
            .setContentTitle("Title")
            .setContentText("You've received a new message.")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setAutoCancel(true)
            .setDefaults(defaults);
            .setLargeIcon(largeIcon)
            .setPriority(Notification.PRIORITY_HIGH);

    /* Notification Icon as per API level*/

      if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mNotifyBuilder.setSmallIcon(R.drawable.icon_s);
        //    mNotifyBuilder.setColor(getResources().getColor(R.color.white));
      } else {
        mNotifyBuilder.setSmallIcon(R.mipmap.ic_icon);
      }

    // Set pending intent
     mNotifyBuilder.setContentIntent(resultPendingIntent);
    // Post a notification
    mNotificationManager.notify(NOTIFICATION_NOTICE, mNotifyBuilder.build());
  } 
  else if(type.equalsignoreCase("News")){
     NotificationCompat.Builder mNotifyBuilder;
     NotificationManager mNotificationManager;

     Intent resultIntent = new Intent(this, NewsActivity.class);
     resultIntent.putExtra("msg", msg);
     PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_ONE_SHOT);

     Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_icon);

     mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

     // Set Vibrate, Sound and Light
     int defaults = 0;
     defaults = defaults | Notification.DEFAULT_LIGHTS;
     defaults = defaults | Notification.DEFAULT_VIBRATE;
     defaults = defaults | Notification.DEFAULT_SOUND;


     mNotifyBuilder = new NotificationCompat.Builder(this)
            .setContentTitle("Title")
            .setContentText("You've received a new message.")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setAutoCancel(true)
            .setDefaults(defaults);
            .setLargeIcon(largeIcon)
            .setPriority(Notification.PRIORITY_HIGH);

    /* Notification Icon as per API level*/

      if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mNotifyBuilder.setSmallIcon(R.drawable.icon_s);
        //    mNotifyBuilder.setColor(getResources().getColor(R.color.white));
      } else {
        mNotifyBuilder.setSmallIcon(R.mipmap.ic_icon);
      }

    // Set pending intent
     mNotifyBuilder.setContentIntent(resultPendingIntent);
    // Post a notification
    mNotificationManager.notify(NOTIFICATION_NEWS , mNotifyBuilder.build());
  }
}
}

答案 1 :(得分:0)

答案 2 :(得分:0)

下面, 无需两个Push通知类或方法。

与您的网络团队讨论并告诉他们提供新密钥,他们会在其中为您提供通知类型。(即通知或新闻)

即 {type ="注意",消息="我的留言"};

并且

{type =" News",message ="我的留言"};

答案 3 :(得分:0)

使用此技巧解决 -

if (ApplicationConstants.MSG_KEY.equals("message")) {

                    String strNotification = ("" + extras.get(ApplicationConstants.MSG_KEY));
                    String arr[] = strNotification.split(" ", 2);
                    String firstWord = arr[0];
                    System.out.println("string: " + firstWord);

                    if (firstWord.equalsIgnoreCase("NOTICE:")) {
                        sendNotification("" + extras.get(ApplicationConstants.MSG_KEY));
                    } else if (firstWord.equalsIgnoreCase("NEWS:")) {
                        sendNotificationNews("" + extras.get(ApplicationConstants.MSG_KEY));
                    }

                }