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

舒汉姆·楚(Shubham Chugh)

我是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());
}
Jitesh prajapati

为此功能,您必须生成2个具有不同通知ID的不同通知。

让我解释

为通知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());
  }
}
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

不使用GCM的Android推送通知

来自分类Dev

如何在Android中处理多个推送通知

来自分类Dev

Android GCM推送通知如何处理?

来自分类Dev

在Android Phonegap应用中处理推送通知消息

来自分类Dev

如何在Phonegap上使用GCM处理android中的多个推送通知

来自分类Dev

GCM新的推送通知替换了Android中的旧通知数据

来自分类Dev

适用于Android和推送通知的Google Cloud Messaging GCM

来自分类Dev

GCM推送通知

来自分类Dev

AngularJS和PubNub GCM推送通知

来自分类Dev

GCM推送通知问题

来自分类Dev

从Android中的GCM推送通知获取当前上下文

来自分类Dev

在Android上模拟GCM推送通知

来自分类Dev

WhatsApp是否使用GCM进行推送通知?

来自分类Dev

使用GCM的Android推送通知是免费的

来自分类Dev

使用GCM推送通知发布数据

来自分类Dev

使用Django推送通知

来自分类Dev

如何在Android推送通知中同时使用GCM和Urban Airship

来自分类Dev

不使用GCM的Android推送通知

来自分类Dev

如何在Android中处理多个推送通知

来自分类Dev

在Android中处理解析推送通知

来自分类Dev

GCM和APNS中的推送通知配额

来自分类Dev

GCM推送通知

来自分类Dev

如何使用GCM使推送通知在Android上正常工作?

来自分类Dev

带有GCM的Android推送通知

来自分类Dev

如何使用WinJS处理UWP中的WNS(推送通知)

来自分类Dev

Android通知使用GCM和带有JSON消息的Java将其推送为空

来自分类Dev

使用GCM的Android推送通知是免费的

来自分类Dev

GCM推送通知在Android App中不起作用

来自分类Dev

同时使用HTTP和XMPP协议的GCM / FCM推送通知