如何将Pending Intent添加到此活动

时间:2018-01-18 19:52:29

标签: android android-intent android-pendingintent

我在点击通知时尝试运行活动时遇到问题。 如何以及在何处创建待定意图,在点击通知后必须运行我的应用程序..?我没有通知问题。但点击后我的通知不起作用。请帮忙,谢谢.............................................. .................................................. .................................................. .................................................. ..........................................

package pl.wat.pz.myapplication;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.NotificationCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class MainActivity extends Activity {
private static final String GROUP_UPDATES="group_updates";
private static final String CHANNEL_CONTENT="channel_content";
private static final int NOTIF_ID_CONTENT=1337;
private NotificationManager mgr;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mgr=getSystemService(NotificationManager.class);

    if (mgr.getNotificationChannel(CHANNEL_CONTENT)==null) {

        initContentChannel();
    }

    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.actions, menu);

    return(super.onCreateOptionsMenu(menu));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId()==R.id.settings) {
        Intent i=new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
        i.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
        startActivity(i);
    }

    return super.onOptionsItemSelected(item);
}

public void raiseContent(View view) {
    Notification n=new NotificationCompat.Builder(MainActivity.this, CHANNEL_CONTENT)
            .setContentTitle(getString(R.string.notif_content_title))
            .setContentText(getString(R.string.notify_content_text))
            .setSmallIcon(android.R.drawable.stat_sys_warning)
            .build();

    mgr.notify(NOTIF_ID_CONTENT, n);
}

private void initContentChannel() {
    NotificationChannel channel=
            new NotificationChannel(CHANNEL_CONTENT,
                    getString(R.string.channel_name_content),
                    NotificationManager.IMPORTANCE_LOW);

    channel.setGroup(GROUP_UPDATES);
    mgr.createNotificationChannel(channel);
}
}

1 个答案:

答案 0 :(得分:0)

您对类的使用意图扩展FirebaseMessagingService,您的意图代码如下所示:

Intent intent = new Intent(context, yourClass.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        final PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        context,
                        requestCode,
                        intent,
                        PendingIntent.FLAG_CANCEL_CURRENT
                );

//然后设置为通知

NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
bigPictureStyle.setBigContentTitle(title);
bigPictureStyle.setSummaryText(Html.fromHtml(message).toString());
bigPictureStyle.bigPicture(bitmap);
Notification notification;
        notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
                .setAutoCancel(true)
                .setContentTitle(title)
                .setContentIntent(resultPendingIntent)
                .setSound(alarmSound)
                .setStyle(bigPictureStyle)
                .setWhen(getTimeMilliSec(timeStamp))
                .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
                .setContentText(message)
                .build();

        NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(Config.NOTIFICATION_ID_BIG_IMAGE, notification);