如果点击通知怎么打开另一个活动?

时间:2014-07-12 10:46:32

标签: android

 public class SimpleService extends Service {
 private NotificationManager mNM;
 private int NOTIFICATION = 0;

 public class LocalBinder extends Binder {
        SimpleService getService() {
            return SimpleService.this;
        }
    }
 @Override
 public IBinder onBind(Intent intent) {
    return mBinder;
 }

@Override
public void onCreate() {
    super.onCreate();
    Toast.makeText(this,"Service created", Toast.LENGTH_LONG).show();        
}

@Override
public void onDestroy() {
    // Cancel the persistent notification.
    mNM.cancel(NOTIFICATION);
    // Tell the user we stopped.
    Toast.makeText(this,"Service is destroy", Toast.LENGTH_SHORT).show();
}   

@Override
public void onStart(Intent intent, int startId) {
    super.onCreate();
    Toast.makeText(this,"Service started", Toast.LENGTH_LONG).show();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this,"task perform in service",300).show();
    mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    // Display a notification about us starting.  We put an icon in the status bar.
    showNotification();
    return START_STICKY;
}   

  // This is the object that receives interactions from clients.  See
    // RemoteService for a more complete example.
    private final IBinder mBinder = new LocalBinder();

    /**
     * Show a notification while this service is running.
     */
    private void showNotification() {
        // In this sample, we'll use the same text for the ticker and the expanded notification
        CharSequence text = getText(R.string.local_service_started);

        // Set the icon, scrolling text and timestamp
        Notification notification = new Notification(R.drawable.ic_launcher, text,
                System.currentTimeMillis());
        Intent intent = new Intent(this,NotificationRecieverActivity.class);
        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, "latest information",
                       text, contentIntent);

        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        // Send the notification.
        mNM.notify(NOTIFICATION, notification);
    }

}

如果我点击通知,那么此通知是明确的,但我不会将此通知消息显示给另一个活动,即NotificationRecieverActivity.class,但这不会显示任何内容。请帮帮我。

3 个答案:

答案 0 :(得分:3)

执行以下操作

 Intent intent = new Intent(this,NotificationRecieverActivity.class);
 intent.putExtra("YOURTAG", "DATA");
    // The PendingIntent to launch our activity if the user selects this notification
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, 0);

或使用捆绑

Intent intent = new Intent(this,NotificationRecieverActivity.class);
Bundle bundle = new Bundle();
bundle.putString("YOURTAG", "DATA");
intent.putExtra("BUNDLETAG", bundle);
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, 0);

全班

public class GcmIntentService extends IntentService {
private NotificationManager mNotificationManager;

public GcmIntentService() {
    super("GcmIntentService");
}
public static final String TAG = "Mobien Reception Service";

@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
    // The getMessageType() intent parameter must be the intent you received
    // in your BroadcastReceiver.
    String messageType = gcm.getMessageType(intent);

    if (!extras.isEmpty()) {  // has effect of unparcelling Bundle
        /*
         * Filter messages based on message type. Since it is likely that GCM will be
         * extended in the future with new message types, just ignore any message types you're
         * not interested in, or that you don't recognize.
         */
        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
            makeMessage("Send error: " + extras.toString());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
            makeMessage("Deleted messages on server: " + extras.toString());
        // If it's a regular GCM message, do some work.
        } else {
            Log.d(TAG, "Received Message :"+extras.getString("message"));
            makeMessage(extras.getString("message"));
        }
    }
    // Release the wake lock provided by the WakefulBroadcastReceiver.
    GcmBroadcastReceiver.completeWakefulIntent(intent);
}

private void makeMessage(String msg) {

    if(!msg.equals("") || msg.contains("#")) {
        String temp [] = StringUtility.split(msg, '#');
        String header = temp[0];
        Log.d(TAG, "Header Message :"+header);
        if(header.trim().contains("DLV")) {
            sendNotification("Del. No. "+temp[1], "Against SAP SO.No. "+temp[2], 123);
        } else if(header.trim().contains("PGI")) {
            sendNotification("PGI No. "+temp[1], "Against Del. No."+temp[2], 99);
        } else if(header.trim().contains("INV")) {
            sendNotification("Inv. No. "+temp[1], "Against Del. No."+temp[2], 157);
        }
    }
}

// Put the message into a notification and post it.
// This is just one simple example of what you might choose to do with
// a GCM message. 
private void sendNotification(String contentTitle, String contentText) {

    final Random r = new Random();
    final int notificationId = r.nextInt();

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, LoginActivity.class), 0);

    Bitmap largeIcon= BitmapFactory.decodeResource(getApplicationContext().getResources(), 
            R.drawable.ic_launcher);

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

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.ic_launcher)
    .setLargeIcon(largeIcon)
    .setContentTitle(contentTitle)
    .setContentText(contentText)
    .setAutoCancel(true)
    .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));


    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(notificationId, mBuilder.build());
}

我为我的演示做了些什么。我没有时间根据您的需要进行修改。但它应该帮助你做你正在做的事情。

答案 1 :(得分:0)

尝试下面的意图

Intent a=new Intent(this,NewActivity.class);
a.putExtra("var", "your message"); //value passing
startActivity(a);

并在NewActivity类中获取消息,例如

try {
     Intent i = getIntent();            
     String message = i.getStringExtra("var");          
}catch (Exception ex) {
    Log.e("Error", "Loading exception");
}

答案 2 :(得分:0)

只需将您创建的待处理意图添加到 -

通知中
notification.setContentIntent(contentIntent);
创建PendingIntent后